求助,一个字符串地址的问题!
String s1 = "a";
String s2 = s1+ "b";
String s3 = "a" + "b";
System.out.println(s2 == "ab");
System.out.println(s3 == "ab");
public class StringTest {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String s1 = "a";
String s2 = s1+ "b";
String s3 = "a" + "b";
System.out.println(s2 == "ab");
System.out.println(s3 == "ab");
}
}
public static void main(java.lang.String[]);
Code:
Stack=3, Locals=4, Args_size=1
0: ldc #16; //String a
2: astore_1
3: new #18; //class java/lang/StringBuilder
6: dup
7: aload_1
8: invokestatic #20; //Method java/lang/String.valueOf:(Ljava/lang/Objec
t;)Ljava/lang/String;
11: invokespecial #26; //Method java/lang/StringBuilder."<init>":(Ljava/la
ng/String;)V
14: ldc #29; //String b
16: invokevirtual #31; //Method java/lang/StringBuilder.append:(Ljava/lang
/String;)Ljava/lang/StringBuilder;
19: invokevirtual #35; //Method java/lang/StringBuilder.toString:()Ljava/l
ang/String;
22: astore_2
23: ldc #39; //String ab
25: astore_3
26: getstatic #41; //Field java/lang/System.out:Ljava/io/PrintStream;
29: aload_2
30: ldc #39; //String ab
32: if_acmpne 39
35: iconst_1
36: goto 40
39: iconst_0
40: invokevirtual #47; //Method java/io/PrintStream.println:(Z)V
43: getstatic #41; //Field java/lang/System.out:Ljava/io/PrintStream;
46: aload_3
47: ldc #39; //String ab
49: if_acmpne 56
52: iconst_1
53: goto 57
56: iconst_0
57: invokevirtual #47; //Method java/io/PrintStream.println:(Z)V
60: return
}
public class StringTest {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
final String s1 = "a";
String s2 = s1+ "b";
String s3 = "a" + "b";
System.out.println(s2 == "ab");
System.out.println(s3 == "ab");
}
}
public static void main(java.lang.String[]);
Code:
0: ldc #16; //String a
2: astore_1
3: ldc #18; //String ab
5: astore_2
6: ldc #18; //String ab
8: astore_3
9: getstatic #20; //Field java/lang/System.out:Ljava/io/PrintStream;
12: aload_2
13: ldc #18; //String ab
15: if_acmpne 22
18: iconst_1
19: goto 23
22: iconst_0
23: invokevirtual #26; //Method java/io/PrintStream.println:(Z)V
26: getstatic #20; //Field java/lang/System.out:Ljava/io/PrintStream;
29: aload_3
30: ldc #18; //String ab
32: if_acmpne 39
35: iconst_1
36: goto 40
39: iconst_0
40: invokevirtual #26; //Method java/io/PrintStream.println:(Z)V
43: return
}
第一个和第三个应该是在栈区创建的对象,
第二个是在堆区创建的。
第三局
String s3 = "a" + "b"; 和
String s3 = "ab";
性质一样。
对象不会在栈区,对象引用:那s2是怎么获取它的内容的呢?
第一个和第三个应该是在栈区创建的对象,
第二个是在堆区创建的。
第三局
String s3 = "a" + "b"; 和
String s3 = "ab";
性质一样。
有可能我的表达有问题,应该是第一个和第三个对象的引用在栈区,对象的值也在栈区。
栈区是可以存放基本数据类型和字符值的,并且存在栈区的值是共享的
详见:http://blog.csdn.net/rzleilei/article/details/11670061
public static void main(String[] args) {
String s1 = "a";
String s2 = s1 + "b";
String s3 = "a" + "b";
/*
* JVM对于字符串引用,由于在字符串的"+"连接中,有字符串引用存在,而引用的值在程序编译期是无法确定的,即s1 +
* "b"无法被编译器优化,只有在程序运行期来动态分配并将连接后的新地址赋给s2。所以下面程序的结果也就为false。
*/
System.out.println(s2 == "ab");
/*
* "a" "b" 都是一个字符串常量,所以+后的s3也是常量,它们在编译期就被确定了,所以s3就是常量池的一个引用,所以s3 == "ab" 就为true
*/
System.out.println(s3 == "ab");
}