文章目录
前言
深入String拼接
一、String的几种拼接情况
String s1 = "abc";
String s2 = "abc";
System.out.println(s1 == s2);//true
String s3 = "ab"+"c";
System.out.println(s3 == s1);//true
final String s4 = "ab";
final String s5 = "c";
String s6 = s4+s5;
System.out.println(s6 == s1);//true
String s7 = "ab";
String s8 = "c";
String s9 = s8+s7;
System.out.println(s9==s1);//false
两个常量将在编译时直接拼接;常量的分类:数字字面量,字面值常量,final修饰的变量。
两个变量进行拼接时将新建一个StringBuilder对象进行操作,例如:
String s9 = s7+s8;//new StringBuilder(String.value(s7)).append(s8).toString();
将s7转换为String对象,在传入StringBuilder,再使用append()方法增加,最后转换为String对象赋值给s9。

292

被折叠的 条评论
为什么被折叠?



