There is an interview question:
How many instances will be created in this statement:
String str1 = new String ("abc")
And the answer is 2: str1
and "abc"
.
Is that correct?
There is an interview question:
How many instances will be created in this statement:
String str1 = new String ("abc")
And the answer is 2: str1
and "abc"
.
Is that correct?
Only one instance will be created at run time . When the class is loaded the literal "abc"
will be interned in the String pool , though technically speaking JVM creates an instance of "abc"
and keeps it in the String pool . The expression new String("abc")
creates an instance of the String
.
A string literal is a reference to an instance of class String (§4.3.1, §4.3.3).
Moreover, a string literal always refers to the same instance of class String. This is because string literals - or, more generally, strings that are the values of constant expressions (§15.28) - are "interned" so as to share unique instances, using the method String.intern.
Also , read JLS 15.28
Compile-time constant expressions of type String are always "interned" so as to share unique instances, using the method String.intern.
Suggested Reading:
No - only one instance has been created in the heap. The literal "abc" goes to the pool. Compile and do javap and you will see only one new in bytecode.
Consider this class-
public class InstanceTest {
public static void main(String... args){
String str1 =new String ("abc");
}
}
If we compile and do-
javap -c InstanceTest
We get-
public class InstanceTest {
public InstanceTest();
Code:
0: aload_0
1: invokespecial #1 // Method java/lang/Object."<init>":()V
4: return
public static void main(java.lang.String...);
Code:
0: new #2 // class java/lang/String
3: dup
4: ldc #3 // String abc
6: invokespecial #4 // Method java/lang/String."<init>":(Ljava/lang/String;)V
9: astore_1
10: return
}
str1 is a variable reference, not Object and "abc" is String literal, not Object
I think, Only one instances is created on Heap
the new keyword create only one instance and, in your example, you put the reference of the object into str1 (note: otherwise it would be deleted by the garbage collector).
You will create two String instances. Using the string constructors rather than declaring them as literals will always create distinct instances.
Just try the following code :
String str = "abc";
String str2 = "abc";
String newString = new String(str);
System.out.println("Are " + str + " and " + newString + " the same instance? " + (str == newString));
System.out.println("Are " + str + " and " + str2 + " the same instance? " + (str == str2));
Only one instance of string will be created, since java compiler has the intelligence to avoid unwanted creation of String objects.It will compile the file to a class file in which it tries to optimize the code as much as possible.
Read about string interning here http://en.wikipedia.org/wiki/String_interning
2 is correct,When use "abc" Java const pool create a "abc" instance,Use new("abc"), it create another String instance in heap.