String a="abc";
String b="abc";
System.out.println(a==b);
String b="abc";
System.out.println(a==b);
|
TroevilMar 24, 2017
1
String a="abc";
String b="abc"; System.out.println(a==b); String c=new String("abc"); System.out.println(b==c); |
|
misaka19000Mar 24, 2017 via Android
2
我想起来 r 大的那篇文章了
|
|
3
所以最简单的不就是把用=判断相等的打死不就完了
|
|
ChrisplusMar 24, 2017
4
'''
In computer science, string interning is a method of storing only one copy of each distinct string value, which must be immutable. Interning strings makes some string processing tasks more time- or space-efficient at the cost of requiring more time when the string is created or interned. The distinct values are stored in a string intern pool. ''' |
|
shalkMar 25, 2017
5
== 用于基本类型的比较,对象会直接比较引用
对象建议用 equals 比较,但需要实现这个方法。 String 作为对象也按照这个规则,但是它有一个 String Pool 的概念( http://stackoverflow.com/questions/3052442/what-is-the-difference-between-text-and-new-stringtext ) |
|
cs4814751Mar 28, 2017
6
|
|
cwekMar 28, 2017
7
String 是对象,==对于对象判断引用一致。
字面 String 常量,还要包括编译器常量折叠后的,通过常量池实现引用一致,通过运行期 new 出来的都在堆中,肯定引用不一致。 intern()方法在 java 版本有不同 运行期 String.intern()会查找 String 缓存池有没相同内容的,有则返回相同引用。 但: 1.6 及之前,没则复制一份到 String 常量池,并返回引用,引用对象相同。 1.7 及之后,没则复制一个堆引用到 String 常量池并返回,实际返回的还是堆引用,与常量引用不同。 |
|
8
|
|
misaka19000Mar 28, 2017 via Android
9
@huiyue 只能说你们公司技术太烂,面试的都是些什么人
|
|
10
@misaka19000 很有道理。
|