【发布时间】:2013-01-05 00:55:57
【问题描述】:
我有两个问题:
public static void main(String[] args) {
String s1 = "bla";
String s2 = "b" +"l" + "a";
String s3 = "b".concat("l").concat("a");
if(s1 == s2)
System.out.println("Equal");
else
System.out.println("Not equal");
if(s1 == s3)
System.out.println("Equal");
else
System.out.println("Not equal");
}
为什么
s1和s2指向同一个对象,而s1和s3却没有? (没有使用new关键字)。-
如果我从用户那里得到一个字符串并将这些行添加到上面的代码中:
BufferedReader in=new BufferedReader(new InputStreamReader(System.in)); String name=in.readLine(); if(name.equals("test")) s1 = s1 + "xyz";如果用户输入
xyz,程序将打印Not equal,当用户输入另一个东西时,程序输出Equal。这是否意味着池会随着整个程序的执行而改变?优化器是否在编译时工作并在runtime中继续工作?
【问题讨论】: