今天突然想到了一个问题:一个对象默认的toString方法,返回的String是放在常量池中的,还是堆中的
就做了个实验
Integer one_1 = new Integer(1);
String str_one = one_1.toString();
String one_2 = "1";
String one_3 = new String("1");
System.out.println("str_one = "+str_one);
System.out.println("one_2 = "+one_2);
System.out.println("one_3 = "+one_3);
System.out.println("one_1 == one_2 "+(str_one == one_2));
System.out.println("one_1 == one_3 "+(str_one == one_3));
System.out.println("str_one.equals(one_3) "+str_one.equals(one_3));
System.out.println("one_2 == one_3 "+(one_2 == one_3));
输出
str_one = 1
one_2 = 1
one_3 = 1
one_1 == one_2 false
one_1 == one_3 false
str_one.equals(one_3) true
one_2 == one_3 false
证明了:对象默认的toString方法,返回的String是放在堆中的String对象