Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String s = new String("xyz"). How many objects has been made after this line of code execute?

Tags:

java

jvm

The commonly agreed answer to this interview question is that two objects are created by the code. But I don't think so; I wrote some code to confirm.

public class StringTest {
    public static void main(String[] args) {
        String s1 = "a";
        String s2 = "a";
        String s3 = new String("a");
        System.out.println("s1: "+s1.hashCode());
        System.out.println("s2: "+s2.hashCode());
        System.out.println("s3: "+s3.hashCode());
    }
}

The output is:

Application output

Does this mean that only one object was created?

Reaffirm: My question is how many object was created by the following code:

String s = new String("xyz")

Instead of the StringTest code.

Inspired by @Don Branson, I debugged the below code:

public class test {
    public static void main(String[] args) {
        String s = new String("abc");
    }
}

And the result is:

Enter image description here

The id of s is 84, and the id of "abc" is 82. What exactly does this mean?

like image 607
smileVann Avatar asked Oct 30 '13 01:10

smileVann


People also ask

How many objects are created string s new string?

The answer is: 2 String objects are created.

How many objects will be created for the Java program string s new string learning?

By new keyword : Java String is created by using a keyword “new”. For example: String s=new String(“Welcome”); It creates two objects (in String pool and in heap) and one reference variable where the variable 's' will refer to the object in the heap.

How many objects are created using new keyword?

Using the new keyword in java is the most basic way to create an object. This is the most common way to create an object in java. Almost 99% of objects are created in this way.

How many types of object will get created when you create string in below format?

Since, it is existing, no new object will be created. Hence reference "d" points to existing object "examveda". So ultimately 3 object will be created at the end.


3 Answers

THERE ARE ERRORS BELOW DEPENDING ON THE JVM/JRE THAT YOU USE. IT IS BETTER TO NOT WORRY ABOUT THINGS LIKE THIS ANYWAYS. SEE COMMENTS SECTION FOR ANY CORRECTIONS/CONCERNS.

First, this question really asks about this addressed here: Is String Literal Pool a collection of references to the String Object, Or a collection of Objects

So, that is a guide for everyone on this matter.

...

Given this line of code: String s = new String(“xyz”)

There are two ways of looking at this:

(1) What happens when the line of code executes -- the literal moment it runs in the program?

(2) What is the net effect of how many Objects are created by the statement?

Answer:

1) After this executes, one additional object is created.

a) The "xyz" String is created and interned when the JVM loads the class that this line of code is contained in.

  • If an "xyz" is already in the intern pool from some other code, then the literal might produce no new String object.

b) When new String s is created, the internal char[] is a copy of the interned"xyz" string.

c) That means, when the line executes, there is only one additional object created.

The fact is the "xyz" object will have been created as soon as the class loaded and before this code section was ever run.

...next scenario ...

2) There are three objects created by the code (including the interned "a")

String s1 = "a"; String s2 = "a"; String s3 = new String("a"); 

a) s1 and s2 are just referenced,not objects, and they point to the same String in memory.

b) The "a" is interned and is a compound object: one char[] object and the String object itself. It consisting of two objects in memory.

c) s3, new String("a") produces one more object. The new String("a") does not copy the char[] of "a", it only references it internally. Here is the method signature:

public String2(String original) {         this.value = original.value;         this.hash = original.hash; } 

One interned String ("a") equals 2 Objects. And one new String("a") equals one more object. Net effect from code is three objects.

like image 181
The Coordinator Avatar answered Sep 22 '22 23:09

The Coordinator


Two objects will be created for this:

String s = new String("abc");

One in the heap and the other in the "string constant pool" (SCP). The reference s will pointing to s always, and GC is not allowed in the SCP area, so all objects on SCP will be destroyed automatically at the time of JVM shutdown.

For example:

Here by using a heap object reference we are getting the corresponding SCP object reference by call of intern()

String s1 = new String("abc");
String s2 = s1.intern(); // SCP object reference
System.out.println(s1==s2); // false
String s3 = "abc";
System.out.println(s2==s3); //True s3 reference to SCP object here
like image 31
user2663609 Avatar answered Sep 21 '22 23:09

user2663609


  1. String s = new String("xyz");

The above line will create two object one is in heap and another is in String constant pool.

now if we do this

  1. String s = new String("xyz");
  2. String s1 ="xyz";

the above two statement will create two object. The first line String s = new String("xyz");will create two object as mentioned in 1st line and , When String s = "xyz";executes it checks in string constant pool if there is same content object is there or not, since the first line made an entry in string constant pool with "xyz" it returns the same reference and does not create other object.

What if we have these four line together as mentioned bellow.

  1. String s2 = new String("xyz");
  2. String s3 ="xyz";
  3. String s4 = new String("xyz");
  4. String s5 ="xyz";

If we execute the above line we will have three object.

  • The first and as mentioned will create two object one in heap and another in String constant poll.
  • When the second line executes it checks in the string constant poll
    and find with "xyz" so it returns the same object, so till second line we have two objects.
  • when the third line executes it will create a new object in the heap since new operator creates object in the heap so till third line will have 3 objects.
  • When the fourth line executes it checks in the string constant poll
    and find with "xyz" so it returns the same object, so fourth line we have three objects.

Bonus about the intern() method

When the intern() method is invoked on a String object it looks the string contained by this String object in the pool, if the string is found there then the string from the pool is returned. Otherwise, this String object is added to the pool and a reference to this String object is returned.

public class TestString {

    public static void main(String[] args) {
        String s1 = "Test";
        String s2 = "Test";
        String s3 = new String("Test");
        final String s4 = s3.intern();
        System.out.println(s1 == s2);
        System.out.println(s2 == s3);
        System.out.println(s3 == s4);
        System.out.println(s1 == s3);
        System.out.println(s1 == s4);
        System.out.println(s1.equals(s2));
        System.out.println(s2.equals(s3));
        System.out.println(s3.equals(s4));
        System.out.println(s1.equals(s4));
        System.out.println(s1.equals(s3));
    }

}

//Output
true
false
false
false
true
true
true
true
true
true

See the magic of intern by applying intern method on new string object. intern is applied here so it will check if "Test" is available in String Constant pool or not since "Test" is available in String constant pool and it will return the same object so s3 has the same reference as s1 and s2 and will get all the result as true

public class TestString {

    public static void main(String[] args) {
          String s1 = "Test";
	        String s2 = "Test";
	        String s3 = new String("Test").intern(); 
	        final String s4 = s3.intern();
	        System.out.println(s1 == s2);
	        System.out.println(s2 == s3);
	        System.out.println(s3 == s4);
	       System.out.println(s1 == s3);
	        System.out.println(s1 == s4);
	        System.out.println(s1.equals(s2));
	        System.out.println(s2.equals(s3));
	        System.out.println(s3.equals(s4));
	        System.out.println(s1.equals(s4));
	        System.out.println(s1.equals(s3));
    }

}

true
true
true
true
true
true
true
true
true
true
like image 30
Varun Avatar answered Sep 21 '22 23:09

Varun