String字符串拼接


結論:常量與常量的拼接結果在常量池,且常量池中不會存在相同內容的常量。

   只要其中有一個是變量,結果就在堆中。

   如果拼接結果調用intern()方法,返回值就在常量池中。

// 示例

import org.junit.Test;

/**
* @auto dh
* @create 2020-03-24-12:16
*/
public class StringDemo003 {
@Test
public void SDemo(){
String s1="hello";
String s2="World";
String s3="helloWorld";
String s4=s1+"World";
String s5="hello"+s2;
String s6=s1+s2;
String s7="hello"+"World";
String s8=(s1+s2).intern();
System.out.println(s3==s4);//false
System.out.println(s3==s5);//false
System.out.println(s3==s6);//false
System.out.println(s3==s7);//true
System.out.println(s3==s8);//true
}
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM