本帖最后由 彭坤 于 2012-2-11 17:15 编辑
3个,两次new String("XYZ")产生两个对象,给s赋值以后,相当创建了一个对象,1+2=3
做了个测试,把代码和结果贴上来供大家观摩
public class Test {
public static void main(String[] args) {
String s = "";
System.out.println("s:"+s.hashCode());
String s1 = new String("XYZ");
String s2 = new String("XYZ");
s=s1+s2;
System.out.println("s1:"+s1.hashCode());
System.out.println("s2:"+s2.hashCode());
System.out.println("s:"+s.hashCode());
}
}
-----------------------运行结果--------------------------
s:0
s1:87417
s2:87417
s:-1690640032
|