使用新运算符创建了多少个对象?

时间:2011-07-09 09:08:50

标签: java

  

可能重复:
  Java Strings: “String s = new String(”silly“);”

如果我写

String s= new String("how many object b created by this method ");

与这样做相比,将创建多少个参考对象和对象:

Sting s1="Is this method is good as compare to upper"; 

5 个答案:

答案 0 :(得分: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   首先检查字符串文字池。   如果字符串已经存在于   游泳池,汇集的参考   实例返回。如果字符串   池中不存在新的String   对象实例化,然后放入   游泳池。 Java可以做到这一点   字符串的优化   不可变的,可以不用共享   担心数据损坏。

     

source

以上概念与string interning有关;所有文字字符串和字符串值常量表达式都在Java [source]中实现。所以基本上,只有当String s1="Is this method is good as compare to upper";不在池中时,使用"Is this method is good as compare to upper"才会创建新对象。

答案 1 :(得分:2)

使用String s1="some string"不会创建新的String对象。每个String文字都有现有的String对象。

具有相同值的字符串文字用单个String对象表示,因此如果String s1="some string"; String s2="some string";同时使用s1s2引用相同的"some string"对象。

new String("...")创建一个新的String对象,它使用与String对象相同的数据,将值“...”传递给构造函数。

答案 2 :(得分: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.equals而不是String时应始终使用==

但是不要相信我的话......请查看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. 

答案 3 :(得分:1)

在Java中创建String对象有两种方法:

  • 使用new运算符。例如,

    String str = new String(“Hello”);

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

    String str =“Hello”; (字符串文字)或
    String str =“Hel”+“lo”; (字符串常量表达式)。

字符串文字池:

  

字符串分配,就像所有对象一样   分配,在两个时间都证明是昂贵的   和记忆。 JVM执行一些操作   实例化字符串时的欺骗   文字以提高绩效和   减少内存开销。削减   创建的String对象数   在JVM中,String类保持一个   字符串池。每次你的代码   创建一个字符串文字,JVM   首先检查字符串文字池。   如果字符串已经存在于   游泳池,汇集的参考   实例返回。如果字符串   池中不存在新的String   对象实例化,然后放入   游泳池。

答案 4 :(得分:0)

使用NEW关键字创建String对象总是在堆中创建一个包含所需字符串的对象,并返回堆中创建的对象的引用。

创建没有NEW关键字的String对象(使用文字)首先检查字符串文字池中具有相同数据的现有字符串,如果找到,则返回String文字池中的相同引用,否则,新的是在String文字池中创建并返回其引用。