程序:
-
-
public
class
Test {
-
public static void main(String args[]) {
-
String s1 =
"abc";
-
String s2 =
"abc";
-
String s3 =
"abc" +
"def";
-
String s4 =
"abcdef";
-
String s5 = s2 +
"def";
-
String s6 =
new String(
"abc");
-
System.
out.println(s1 == s2);
-
System.
out.println(s3 == s4);
-
System.
out.println(s4 == s5);
-
System.
out.println(s4.
equals(s5));
-
System.
out.println(s6.
equals(s1));
-
System.
out.println();
-
System.
out.print(
-
"\ns1:"+System.identityHashCode(s1)+
-
"\ns2:"+System.identityHashCode(s2)+
-
"\ns3:"+System.identityHashCode(s3)+
-
"\ns4:"+System.identityHashCode(s4)+
-
"\ns5:"+System.identityHashCode(s5)+
-
"\ns6:"+System.identityHashCode(s6));
-
-
}
-
}
内存模型图:
结果:
-
true
-
true
-
false
-
true
-
true
-
-
s1
:557041912
-
s2
:557041912
-
s3
:1134712904
-
s4
:1134712904
-
s5
:985922955
-
s6
:1435804085
-
由结果可知
-
-
s1和s2指向的地址都是常量池中的
"abc"
-
s3和s4指向的地址都是常量池中的
"abcdef"
-
s5和s3并不指向同一个地址,其实s5指向的是对象内存区,也就是s5其实指向了一个对象
-
s6同理.
-
-
分析:
-
字符串用
"+"拼接的时候,如果是两个常量拼接,,不是通过某种方式将两个字符串的地址进行处理,连接两个字符串,而是直接产生一个由两个字符串拼接的新字符串常量;
-
字符串用
"+"拼接的时候,如果有变量参与拼接,则产生一个新对象
注意:
String中的“+”运算符本质上是StringBuffer的append()方法(StringBuilder append(String)),所以每次拼接都会新建一个对象,效率极低,而且要注意一个问题,就是方法的运算优先级是最高的,所以极有可能出现意外情况,见下面的例子
原始数据类型会转换成包装类
eg:String s = 1 + "sss";
这里的1本来是int型常量,在进行拼接时,Java进行了类型升级,升级成为了Integer类,并调用了Integer类的toString()方法,将1从数字转换成了字符串,然后再进行拼接
所以String在使用“+”进行拼接的时候是非常消耗内存和运行时间的,类似于递归,代码简单,但时间效率和空间效率都非常低
-
-
优先级带来的问题:
-
-
代码:
-
String s2 =
"hello";
-
String s3 =
"hello";
-
-
-
System.out.
println(
"s2==s3 ?"+s2==s3);
-
System.out.
println(
"s2==s3 ? "+(s2==s3));
-
-
-
结果:
-
-
flase(并没有前面的字符串,而且必定
false,无论是什么==什么)
-
s2==s3 ?
true
-
-
-
-
结果分析:
-
-
"s2==s3 ?"+s2==s3
-
等价于:
-
StringBuilder sb1 =
new StringBuilder(
"s2==s3 ?");
-
sb1.
append(s2);
-
String temp = sb1.toString();
-
boolean
bool= temp == s3?
true:flase;
-
String stemp=
bool.toString;
-
System.out.
println(stemp);
-
-
-
-
-
-
-
-
-
-
注意:字面值连接怒徽降低效率,因为编译器会优化
-
eg:String s =
"asf"+
"sfdf"+
"djkd"+
"dsd";
//这个效率高
-
-
-
boolean tempbool = sb1 ==sb2;
-
-
StringBuilder sb3 =
new StringBuilder(tempbool.toString());
-
-
System.out.
println(sb3);
-