How many object creates with new operator? [duplicate]
Possible Duplicate:
Java Strings: “String s = new String(”silly“);”
If i write
String s= new String("how many object b created by this method ");
how many reference objects and objects will be created in comparison to doing it this way:
Sting s1="Is this method is good as compare to upper"; 开发者_开发问答
Using String s= new String("how many object b created by this method ");
creates a new object 's' of String class, and you are passing the string "how many object b created by this method" to its constructor.
In String s1="Is this method is good as compare to upper";
's1' is a string literal. On string literals:
Each time your code create a string literal, the JVM checks the string literal pool first. If the string already exists in the pool, a reference to the pooled instance returns. If the string does not exist in the pool, a new String object instantiates, then is placed in the pool. Java can make this optimization since strings are immutable and can be shared without fear of data corruption.
source
The above concept is related to string interning; all literal strings and string-valued constant expressions are interned in Java [source]. So basically, using String s1="Is this method is good as compare to upper";
will create a new object only if "Is this method is good as compare to upper"
is not already in the pool.
Using String s1="some string"
doesn't create new String object. There is existing String object for every String literal already.
String literals with same values are represented with single String object, so if you use String s1="some string"; String s2="some string";
both s1
, s2
refer to same "some string"
object.
new String("...")
creates one new String object, which uses same data as String object for value "..." passed to constructor.
Consider:
String s1 = new String("hi");
String s2 = new String("hi");
System.out.println(s1 == s2);
Will print false
.
However
String s1 = "hi";
String s2 = "h1";
System.out.println(s1 == s2);
Will print true
.
And
String s1 = "hi";
String s2 = new String("hi");
System.out.println(s1 == s2);
Will print false
.
This is why you should always use String.equals
when comparing String
s instead of ==
.
But don't take my word for it... Check this excerpt from the Java Language Specification 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.
There are two ways to create a String object in Java:
Using the new operator. For example,
String str = new String("Hello");
Using a string literal or constant expression). For example,
String str="Hello"; (string literal) or
String str="Hel" + "lo"; (string constant expression).
String Literal Pool :
String allocation, like all object allocation, proves costly in both time and memory. The JVM performs some trickery while instantiating string literals to increase performance and decrease memory overhead. To cut down the number of String objects created in the JVM, the String class keeps a pool of strings. Each time your code create a string literal, the JVM checks the string literal pool first. If the string already exists in the pool, a reference to the pooled instance returns. If the string does not exist in the pool, a new String object instantiates, then is placed in the pool.
Creating a String object using NEW keyword always creates an object in the heap containing the desired string and the reference of the created object in the heap is returned.
Creating a String object without NEW keyword (using literal) first checks for an existing string with the same data in the String literal pool, if found, the same reference from the String literal pool is returned, else, a new one is created in the String literal pool and its reference is returned.
精彩评论