6

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?

Clark
  • 1,357
  • 1
  • 7
  • 18
XiaoYao
  • 3,177
  • 4
  • 22
  • 19
  • I feel the answer is 1 because "abc" gets interned, and the only "instance" gets created is `str1` in heap – sanbhat Jul 29 '13 at 09:40

7 Answers7

7

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.

JLS 3.10.5:

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:

  1. Java String is Special
  2. what is String pool in java?
  3. Strings, Literally.
Community
  • 1
  • 1
AllTooSir
  • 48,828
  • 16
  • 130
  • 164
  • +1. I think the term "intern" confuses folks. If "abc" was already created and placed in the sting pool, jvm doesn't have to create one. It can just refer to the existing one. If doesn't exist, it will have to create one and place it to the pool. And then "new" keyword also adds to the confusion when the statement is like "when you instantiate with "new" keyword, a new object is created"..which makes it sound like "abc" wouldn't be created ever since mighty "new" was not used. "abc" is just magically there. – Sajal Dutta Jul 29 '13 at 10:41
  • String s1 = new String("hello"); , how many references are created at JVM level? I know of two objects:1 at the time of class loading for "hello" and 1 at the actual time of execution of command. 1 reference is s1, my doubt is there is also a 2nd reference s2 which is the result of "hello" string at class loading which is passed to the "new String()" constructor. In java, there is no way we can pass an object itself in a function or constructor, we can only pass a reference. Also, there should be a 3rd reference s3, which should be the result of new String("hello") and then got assigned to s1? – Bhavesh Agarwal Jan 16 '14 at 04:44
0

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        
}
Sajal Dutta
  • 18,272
  • 11
  • 52
  • 74
0

str1 is a variable reference, not Object and "abc" is String literal, not Object

I think, Only one instances is created on Heap

ThiepLV
  • 1,219
  • 3
  • 10
  • 21
0

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).

0

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));
C.Champagne
  • 5,381
  • 2
  • 23
  • 35
0

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

Subin Sebastian
  • 10,870
  • 3
  • 37
  • 42
0

2 is correct,When use "abc" Java const pool create a "abc" instance,Use new("abc"), it create another String instance in heap.

coonooo
  • 205
  • 1
  • 4