当前位置: 技术问答>java相关
大家来讨论一下关于String对象的"=="问题!
来源: 互联网 发布时间:2015-07-04
本文导语: public class Test { public static void main(String[] args) { String hello = "Hello", lo = "lo"; System.out.print((hello == "Hello") + " "); System.out.print((Other.hello == hello) + " "); System.out.print((Other.hello == hello) + " "); System.out.print((h...
public class Test {
public static void main(String[] args) {
String hello = "Hello", lo = "lo";
System.out.print((hello == "Hello") + " ");
System.out.print((Other.hello == hello) + " ");
System.out.print((Other.hello == hello) + " ");
System.out.print((hello == ("Hel"+"lo")) + " ");
System.out.print((hello == ("Hel"+lo)) + " ");
System.out.println(hello == ("Hel"+lo).intern());
}
}
class Other{
static String hello="Hello";
}
首先大家先想想会打印出来一些什么信息,再讨论一下特别是最后一个“.intern()”方法有什么作用!!!!
public static void main(String[] args) {
String hello = "Hello", lo = "lo";
System.out.print((hello == "Hello") + " ");
System.out.print((Other.hello == hello) + " ");
System.out.print((Other.hello == hello) + " ");
System.out.print((hello == ("Hel"+"lo")) + " ");
System.out.print((hello == ("Hel"+lo)) + " ");
System.out.println(hello == ("Hel"+lo).intern());
}
}
class Other{
static String hello="Hello";
}
首先大家先想想会打印出来一些什么信息,再讨论一下特别是最后一个“.intern()”方法有什么作用!!!!
|
在编译时,程序可以优化变量的分配:如只在内存中分配一个字符串“Hello”,以及可以合并常量
public class Test {
public static void main(String[] args) {
String hello = "Hello", lo = "lo";
//将reference 变量hello指向内存"Hello" ,变量lo指向内存"lo";
/*
0 ldc #2
2 astore_1
3 ldc #3
5 astore_2
*/
System.out.print((hello == "Hello") + " ");
//由于 == 实际上是句柄的比较所以是true
/*
6 getstatic #4
9 new #5
12 dup
13 invokespecial #6
16 aload_1
17 ldc #2
19 if_acmpne 26
22 iconst_1
23 goto 27
26 iconst_0
27 invokevirtual #7
30 ldc #8
32 invokevirtual #9
35 invokevirtual #10
38 invokevirtual #11
*/
System.out.print((Other.hello == hello) + " ");
//同样Other.hello 也指向同一个内存"Hello"
/*
41 getstatic #4
44 new #5
47 dup
48 invokespecial #6
51 getstatic #12
54 aload_1
55 if_acmpne 62
58 iconst_1
59 goto 63
62 iconst_0
63 invokevirtual #7
66 ldc #8
68 invokevirtual #9
71 invokevirtual #10
74 invokevirtual #11
*/
System.out.print((Other.hello == hello) + " ");
//同理
System.out.print((hello == ("Hel"+"lo")) + " ");
//编译时,对于两个常量"Hel"和"lo" 编译器已经将之合并为一个,而不必等待运行时合并
/*
116 new #5
119 dup
120 invokespecial #6
123 aload_1
124 ldc #2
126 if_acmpne 133
129 iconst_1
130 goto 134
133 iconst_0
134 invokevirtual #7
137 ldc #8
139 invokevirtual #9
142 invokevirtual #10
145 invokevirtual #11
*/
System.out.print((hello == ("Hel"+lo)) + " ");
//这个没有合并,于是生成一个新的字符串 == 这样两个句柄相比不同
/*
148 getstatic #4
151 new #5
154 dup
155 invokespecial #6
158 aload_1
159 new #5
162 dup
163 invokespecial #6
166 ldc #13
168 invokevirtual #9
171 aload_2
172 invokevirtual #9
175 invokevirtual #10
178 if_acmpne 185
181 iconst_1
182 goto 186
185 iconst_0
186 invokevirtual #7
189 ldc #8
191 invokevirtual #9
194 invokevirtual #10
197 invokevirtual #11
*/
System.out.println(hello == ("Hel"+lo).intern());
//intern 是一个naitive函数。解释可以看文档
/*
Returns a canonical representation for the string object.
A pool of strings, initially empty, is maintained privately by the class String.
When the intern method is invoked, if the pool already contains a string equal to this String object as determined by the equals(Object) method, then the string from the pool is returned. Otherwise, this String object is added to the pool and a reference to this String object is returned.
It follows that for any two strings s and t, s.intern() == t.intern() is true if and only if s.equals(t) is true
*/
/*
200 getstatic #4
203 aload_1
204 new #5
207 dup
208 invokespecial #6
211 ldc #13
213 invokevirtual #9
216 aload_2
217 invokevirtual #9
220 invokevirtual #10
223 invokevirtual #14
226 if_acmpne 233
229 iconst_1
230 goto 234
233 iconst_0
234 invokevirtual #15
*/
}
}
class Other{
static String hello="Hello";
}
public class Test {
public static void main(String[] args) {
String hello = "Hello", lo = "lo";
//将reference 变量hello指向内存"Hello" ,变量lo指向内存"lo";
/*
0 ldc #2
2 astore_1
3 ldc #3
5 astore_2
*/
System.out.print((hello == "Hello") + " ");
//由于 == 实际上是句柄的比较所以是true
/*
6 getstatic #4
9 new #5
12 dup
13 invokespecial #6
16 aload_1
17 ldc #2
19 if_acmpne 26
22 iconst_1
23 goto 27
26 iconst_0
27 invokevirtual #7
30 ldc #8
32 invokevirtual #9
35 invokevirtual #10
38 invokevirtual #11
*/
System.out.print((Other.hello == hello) + " ");
//同样Other.hello 也指向同一个内存"Hello"
/*
41 getstatic #4
44 new #5
47 dup
48 invokespecial #6
51 getstatic #12
54 aload_1
55 if_acmpne 62
58 iconst_1
59 goto 63
62 iconst_0
63 invokevirtual #7
66 ldc #8
68 invokevirtual #9
71 invokevirtual #10
74 invokevirtual #11
*/
System.out.print((Other.hello == hello) + " ");
//同理
System.out.print((hello == ("Hel"+"lo")) + " ");
//编译时,对于两个常量"Hel"和"lo" 编译器已经将之合并为一个,而不必等待运行时合并
/*
116 new #5
119 dup
120 invokespecial #6
123 aload_1
124 ldc #2
126 if_acmpne 133
129 iconst_1
130 goto 134
133 iconst_0
134 invokevirtual #7
137 ldc #8
139 invokevirtual #9
142 invokevirtual #10
145 invokevirtual #11
*/
System.out.print((hello == ("Hel"+lo)) + " ");
//这个没有合并,于是生成一个新的字符串 == 这样两个句柄相比不同
/*
148 getstatic #4
151 new #5
154 dup
155 invokespecial #6
158 aload_1
159 new #5
162 dup
163 invokespecial #6
166 ldc #13
168 invokevirtual #9
171 aload_2
172 invokevirtual #9
175 invokevirtual #10
178 if_acmpne 185
181 iconst_1
182 goto 186
185 iconst_0
186 invokevirtual #7
189 ldc #8
191 invokevirtual #9
194 invokevirtual #10
197 invokevirtual #11
*/
System.out.println(hello == ("Hel"+lo).intern());
//intern 是一个naitive函数。解释可以看文档
/*
Returns a canonical representation for the string object.
A pool of strings, initially empty, is maintained privately by the class String.
When the intern method is invoked, if the pool already contains a string equal to this String object as determined by the equals(Object) method, then the string from the pool is returned. Otherwise, this String object is added to the pool and a reference to this String object is returned.
It follows that for any two strings s and t, s.intern() == t.intern() is true if and only if s.equals(t) is true
*/
/*
200 getstatic #4
203 aload_1
204 new #5
207 dup
208 invokespecial #6
211 ldc #13
213 invokevirtual #9
216 aload_2
217 invokevirtual #9
220 invokevirtual #10
223 invokevirtual #14
226 if_acmpne 233
229 iconst_1
230 goto 234
233 iconst_0
234 invokevirtual #15
*/
}
}
class Other{
static String hello="Hello";
}