String 物件個數
阿新 • • 發佈:2020-12-21
來源:https://www.cnblogs.com/taochen-go/p/9475947.html 【記憶體圖畫的很好】
String s = new String(“hello”)和String s = “hello”;
String s = new String(“hello”)會建立2(1)個物件,String s = “hello”建立1(0)個物件。
注:當字串常量池中有物件hello時括號內成立!
public class StringDemo2 { public static void main(String[] args) { String s1 = new String("hello"); String s2 = "hello"; System.out.println(s1 == s2);// false System.out.println(s1.equals(s2));// true } } **執行結果:** > false > true
- String s = new String(“hello”)會建立2(1)個物件,String s = “hello”建立1(0)個物件。
注:當字串常量池中有物件hello時括號內成立! - 字串如果是變數相加,先開空間,在拼接。
- 字串如果是常量相加,是先加,然後在常量池找,如果有就直接返回,否則,就建立