2011-07-09 40 views

回答

2

使用String s1="some string"不會創建新的String對象。每個String字符串都有現有的String對象。

具有相同的值的字符串文字來表示單String對象,因此,如果您使用String s1="some string"; String s2="some string";s1s2指同一"some string"對象。

new String("...")創建一個新的String對象,該對象使用與String對象相同的值傳遞給構造函數的值「...」。

7

使用String s= new String("how many object b created by this method ");創建一個String類的新對象',並且將字符串「由此方法創建的對象數b」傳遞給它的構造函數。

String s1="Is this method is good as compare to upper";'s1'是一個字符串文字。在字符串文字:

每次你的代碼 創建一個字符串,則JVM 檢查字符串常量池第一。 如果該字符串已存在於 池中,則會返回對合並的 實例的引用。如果該池中不存在字符串 ,則將新實例化對象字符串 ,然後將其放入 池中。 Java可以使這個 優化,因爲字符串是 不可變的,可以共享沒有 害怕數據損壞。

source

上述概念涉及string interning;所有文字字符串和字符串值常量表達式均以Java [source]進行實施。所以基本上,只有當"Is this method is good as compare to upper"不在池中時,使用String s1="Is this method is good as compare to upper";纔會創建一個新對象。

+0

String s1 = new String(「hello」); ,在JVM級別創建多少個引用?我知道兩個對象:1在類加載「hello」時,1在實際執行命令時。 1的引用是s1,我的疑問是還有第二個引用s2,它是在類加載時傳遞給「new String()」構造函數的「hello」字符串的結果。在java中,我們不可能在一個函數或構造函數中傳遞一個對象本身,我們只能傳遞一個引用。另外,應該有第三個引用s3,它應該是新String(「hello」)的結果,然後分配給s1? –

1

考慮:

String s1 = new String("hi"); 
String s2 = new String("hi"); 
System.out.println(s1 == s2); 

將打印false

然而

String s1 = "hi"; 
String s2 = "h1"; 
System.out.println(s1 == s2); 

將打印true

而且

String s1 = "hi"; 
String s2 = new String("hi"); 
System.out.println(s1 == s2); 

將打印false

這就是爲什麼在比較String而不是==時應始終使用String.equals

但是,不要把我的話......從Java語言規範JLS 3.10選中此摘錄:

Thus, the test program consisting of the compilation unit (§7.3): 

    package testPackage; 
    class Test { 
      public static void main(String[] args) { 
        String hello = "Hello", lo = "lo"; 
        System.out.print((hello == "Hello") + " "); 
        System.out.print((Other.hello == hello) + " "); 
        System.out.print((other.Other.hello == hello) + " "); 
        System.out.print((hello == ("Hel"+"lo")) + " "); 
        System.out.print((hello == ("Hel"+lo)) + " "); 
        System.out.println(hello == ("Hel"+lo).intern()); 
      } 
    } 
    class Other { static String hello = "Hello"; } 

and the compilation unit: 

    package other; 
    public class Other { static String hello = "Hello"; } 

produces the output: 

    true true true true false true 

This example illustrates six points: 

    Literal strings within the same class (§8) in the same package (§7) represent references to the same String object (§4.3.1). 
    Literal strings within different classes in the same package represent references to the same String object. 
    Literal strings within different classes in different packages likewise represent references to the same String object. 
    Strings computed by constant expressions (§15.28) are computed at compile time and then treated as if they were literals. 
    Strings computed by concatenation at run time are newly created and therefore distinct. 

The result of explicitly interning a computed string is the same string as any pre-existing literal string with the same contents. 
1

有使用Java創建一個String對象有兩種方法:

  • 使用新的操作符。例如,

    String str = new String(「Hello」);

  • 使用字符串文字或常量表達式)。例如,

    String str =「Hello」; (字符串文字)或
    String str =「Hel」+「lo」; (字符串常量表達式)。

字符串文字池:

字符串分配,像所有的對象 分配,證明在這兩個時間 和存儲成本。 JVM執行一些 欺騙,而實例化字符串 文字以提高性能和 減少內存開銷。爲了減少在JVM中創建 的String對象的數量 ,String類保留了一個 字符串池。每當您的代碼 創建字符串文字時,JVM 都會首先檢查字符串文字池。 如果該字符串已存在於 池中,則會返回對合並的 實例的引用。如果該池中不存在字符串 ,則將新實例化對象字符串 ,然後將其放入 池中。

0

使用NEW關鍵字創建String對象總是在包含所需字符串的堆中創建一個對象,並返回堆中創建的對象的引用。

創建一個沒有NEW關鍵字的字符串對象(使用文字)首先檢查字符串文字池中具有相同數據的現有字符串,如果找到,則返回字符串文字池中的相同引用,否則,新的在字符串文字池中創建並返回其引用。

相關問題