c++ 指针拼接字符串_字符串拼接+和concat的区别
生活随笔
收集整理的這篇文章主要介紹了
c++ 指针拼接字符串_字符串拼接+和concat的区别
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
+和concat都可以用來拼接字符串,但在使用上有什么區別呢,先來看看這個例子。
public static void main(String[] args) {// example1String str1 = "s1";System.out.println(str1 + 100);//s1100System.out.println(100 + str1);//100s1String str2 = "s2";str2 = str2.concat("a").concat("bc");System.out.println(str2);//s2abc// example2String str3 = "s3";System.out.println(str3 + null);//s3nullSystem.out.println(null + str3);//nulls3String str4 = null;System.out.println(str4.concat("a"));//NullPointerExceptionSystem.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[] = Arrays.copyOf(value, len + otherLen);str.getChars(buf, len);return new String(buf, true); }看下生成的字節碼:
所以可以得出以下結論:
更多Java好文請關注Java技術棧微信公眾號,在公眾號后臺回復關鍵字:java,以下僅為部分預覽。
- 出場率比較高的一道多線程安全面試題
- Java類初始化順序,大神3個示例帶你躺坑
- switch case 支持的 6 種數據類型!
- 面試常考:Synchronized 有幾種用法?
- Hashtable 為什么不叫 HashTable?
本文原創首發于微信公眾號:Java技術棧(id:javastack),轉載請原樣保留本信息。
總結
以上是生活随笔為你收集整理的c++ 指针拼接字符串_字符串拼接+和concat的区别的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: rocketmq原理_彻底看懂Rocke
- 下一篇: 牛客网SQL篇刷题篇(1-2)