浅谈String中的==和对象中引用对象类型的==
@Test public void test02() { StringBuffer sb = new StringBuffer(); sb.append('a'); sb.append(11); System.out.println(sb.toString()); System.out.println("======字符串的引用=========="); String s1 = new String(); s1="123"; String s2 = new String(); s2="123"; System.out.println(s1); System.out.println(s2); System.out.println(s1==s2); System.out.println(s1=s2); System.out.println("======引用对象类型=========="); Person p = new Person(); p.id = 1; p.name = "zhangsan"; Person p2 = new Person(); p2.id = 1; p2.name = "zhangsan"; System.out.println(p); System.out.println(p2); System.out.println(p==p2); System.out.println(p.equals(p2)); System.out.println(p.equals(p)); System.out.println(p.name.equals(p2.name)); System.out.println(p=p2); } class Person{ private int id; private String name; @Override public String toString() { return "Person{" + "id=" + id + ", name='" + name + '\'' + '}'; } }
都是很基础的东西
今天是无聊了,这些基础的东西跑跑也是好的。
造成这些的原因,是与JVM内存机制有关系。
String 常量池
引用对象类型存在于栈Stack中
------------------------- A little Progress a day makes you a big success... ----------------------------