- 运算符+号实际本质上是一个函数
- 用+字符串相连,因为字符串是不可以改变的,所以将两个字符串连接起来的是创建一个新的对象。
- +只适合于较小的或者大小固定的字符串,不适合运用在大规模场景中。
- 多个字符串相连时我们一般使用StringBuilder类代替
- StringBuilder会预先分配内容大小,如果不够会自动扩容
- append(s)方法将字符串相连
案例:
package org.example.review01;
public class SB {
public static void main(String[] args){
String s1="hello";
String s2="world";
String s3=s1+s2;
System.out.println(s3);
StringBuilder sb=new StringBuilder("hello");
sb.append("world");//s1+s2;
System.out.println(sb.toString());
}
}