字符串拼接+和concat的区别

字符串拼接+和concat的区别

+和concat都可以用来拼接字符串,但在使用上有什么区别呢,先来看看这个例子。

public static void main(String[] args){
	String str1="s1";
	System.out.println(str1 + 100);//s1100
	System.out.println(100 + str1);//100s1
	String str2="s2";
	str2 = str2.concat("a").concat("bc");
	System.out.println(str2);//s2abc



	String str3="s3";
	System.out.println(str3 + null);//s3null
	System.out.println(null + str3);//nulls3

	String str4="s4";
	System.out.println(str4.concat("a"));//NullPointerException
	System.out.println("a".concat(str4));//NullPointerException

}

concat源码:

public String concat(String str){
	int otherLen=str.length();
	if(otherLen==0){
		return this;
	}
	int len=value.length;
	char buf[]=Array.copyOf(value,len+otherLen);
	str.getChars(buf,len);
	return new String(buf,true);
}

所以可以得出以下结论:
+可以是字符串或者数字及其他基本类型数据,而concat只能接收字符串。

+左右可以为null,concat为会空指针。

如果拼接空字符串,concat会稍快,在速度上两者可以忽略不计,如果拼接更多字符串建议用StringBuilder。

从字节码来看+号编译后就是使用了StringBuiler来拼接,所以一行+++的语句就会创建一个StringBuilder,多条+++语句就会创建多个,所以为什么建议用StringBuilder的原因。

posted @ 2022-08-24 15:45  FinallySSR  阅读(80)  评论(0编辑  收藏  举报