Java Programming - Java.lang Class - Discussion

1. 

What will be the output of the program?

String x = new String("xyz");
String y = "abc";
x = x + y;
How many String objects have been created?

[A]. 2
[B]. 3
[C]. 4
[D]. 5

Answer: Option C

Explanation:

Line 1 creates two, one referred to by x and the lost String "xyz". Line 2 creates one (for a total of three). Line 3 creates one more (for a total of four), the concatenated String referred to by x with a value of "xyzabc".


Afin said: (Nov 14, 2011)  
Line 1 creates only 1 reference which will point xyz.

Sanjay Singh said: (Dec 13, 2011)  
I am not able t ounderstand the concept behind the answer.Kindly exlpain the solution in detail.


"Line 1 creates two, one referred to by x and the lost String "xyz"----------Please ellaborate.

Mmintz01 said: (Mar 4, 2012)  
The answer is simple :

java creates an object xyz in memory and then a x object which has the ingredients of object xyz. Then xyz is destryed (lost). So at the end we created 2 object but now we have one !

Raju Rathi said: (May 9, 2012)  
@Mmintz01,
Why not x point to xyz object (in your above e. G. ) directly ? isn't it waste of memory by creating 2 object considering xyz is never going to change by program. Kindly elaborate the advatage of this approach ?

Suhas said: (Aug 20, 2012)  
I am not able to understand the concept behind the answer. Kindly exlpain the solution in detail.

Sonam said: (Sep 5, 2012)  
As eg.

If you write, String s= new String("hello");and check
if(s=="hello") then it will print false because == comapres references and as they are two different objects so line 1 in question will create 2 objects.

Rishi said: (Feb 22, 2013)  
First line creates only reference x and one object and second line creates only another object. Third line do not create any object as it is again initialized to the x but strings are immutable.

So only 2 objects are created in all.

Please correct me if I'm wrong.

Ramesh said: (Mar 1, 2013)  
Only 3 objects are created the first line creates only one object.

Viksonai said: (Mar 16, 2013)  
As We Know String is Immutable, but here after concatenating it does not pointing the same ref in String pool, So its pointing new String in constant pool;.

Harikrishna M said: (Mar 29, 2013)  
There will be 3 objects created, Since the argument "xyz" is created, when its trying to create a new String object it will always check for the value in the "String Pool". If such a string object exists any newly created object of same value will be assigned to that object which contains the same value in string pool, it creates a new object in the string pool only if it doesn't exist.

Deepak said: (Apr 10, 2013)  
"abc", "xyz", x, y and "xyzabc" total 5 objects will be created.

xyz will be lost as x now points to new concatenated string. Hence
5-1 = 4 objects.

Md Zakir said: (Apr 21, 2013)  
There are only 3 objects will be created because.

In First line,

String x = new String("xyz");

"xyz" an object is created in memory which is referenced by x(reference variable).if u get the hasCode() of "Hello" and x both will be same because x is referenced to "xyz"

String y = "abc";

This line create one object "abc" which is referenced by y;

And finally when,
x = x + y;

A new object is created "xyzabc" which is referenced by x.
String class is an Immutable string so it is not changed a new object is created when concatenation is performed and is referenced by x.

Dasoju said: (Dec 9, 2013)  
Please summarize somebody, can we fix the answer as 3 objects.

Please conclude the discussion with proper explanation.

Thanks.

M Pradeep said: (Feb 10, 2014)  
Can anyone show the proof that how 4 objects are getting created. I mean is there any way to show to someone on the machine by executing the program.

Gaurav Kumar said: (Apr 19, 2014)  
Can anyone tell me that why line one creates two object. It should create only one object and this answer is wrong.

Jinesh said: (Aug 8, 2014)  
I believe "xyz" is a string literal and not string object. So line creates only one object. Also, as stated, literal "xyz" is not lost as its sitting in the stringpool. "xyz" is not a string object.

Line 2 is also creating another string literal in string pool and its not string object.

Line 3 (concatenation) creates another string object.

So in all - 2 objects - one at line 1 and another at line 3.

Anandi Shewale said: (Aug 14, 2014)  
I agree that Line 1 creates 2 object, and 2nd line 1 object but how 4th object is created?

Xyzabc said: (Dec 3, 2014)  
@Anandi.

String is immutable so new object xyzabc is created.

Harsha Rao said: (Dec 5, 2014)  
Proof is:

String x = "xyz";

x == "xyz" false.

Because x referring ingredients of xyz but not to xyz.

And "xyz" is another.

1 line>2.
2> line 1.
3>line 1.

Richa said: (Jan 8, 2015)  
String x = new String ("xyz"); /* one object created here "xyz", note: x is a reference variable only */.

String y = "abc"; /* one object created here "abc", note: y is a reference variable only */.

X = x + y; /* one object created here "xyzabc" referenced by existing x variable */.

So, total 3 objects are only created. Do not get confuse between Objects and reference variables.

Javadeveloper said: (Jan 8, 2015)  
Let me be more clear you for making you understand the concept of String Objects created. Happy reading.

Case 1> String s1 = new String ("Hello");

String s2 = new String ("Hello");

It will create 2 objects in heap.

Case 2> String s1 = new String ("Hello");

String s2 = new String ("World");

This will create 2 objects in heap.

Case 3> String s1 = new String ("Hello");

String s2 = "Hello";

This will create one object in heap for first line. For second line, it will check whether "Java" exists in string pool or not. If exists, it won't create a new string. It will return the reference to already existing string in pool.

Case 4> String s1 = "Hello";

String s2 = "Hello";

First line will check whether "Java" exists in string pool or not. If exists, it won't create a new string. It will return the reference to already existing string in pool. Second line will get the reference to already created string. So no new objects if "Java" exists or maximum one object.

I hope now this concept is clear to all. :).

Pankaj Rana said: (Jul 1, 2015)  
Assume we have not created any object in heap till now, when below line of code execute --------> It created 2 objects and one reference.

String s1 = new String ("Hello");

Explanation: Heap memory divided in two parts.

1. String Constant Pool.
2. Non String Constant Pool.

"hello" is String literal it created in String Constant Pool and whenever we create object with new String () it will created in Non String Constant Pool,

new String ("Hello"); literal object and String object created and then linked internally.

String s1 = new String ("Hello");

S1 is reference variable of type String created in stack.

If I write String ss="Rana".

It will check first (before creation) there is any literal object with same literal object ("Rana"), if not found in String Constant Pool it create another object in String Constant Pool.

If I write String rr="Hello" object already created in String Constant pool only referenced assign to rr (not new Object Created).

Bablu said: (Dec 24, 2015)  
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.

Istvan Bohm said: (Feb 16, 2016)  
String x = new String("xyz");

First: The argumentum, "xyz".

Second: We create a new string with the copy constructor of the String class from the argumentum.

Sopan Bagalkar said: (Feb 26, 2016)  
Simple explanation:

String x = new String("xyz");

if (x=="xyz") {
System.out.println("true");
}
else {
System.out.println("false");
}

String y = "abc";
x = x + y;
System.out.println(x);


Output:

False.
xyzabc.

Nira said: (Mar 22, 2016)  
Hi all friends.

The line => "String x = new String("xyz");" will produce two objects. One in the heap area and another one in String constant pool.

And the line => "String y = "abc";" will produce only one in the String constant pool.

Again line =>"x = x + y;" will produce one more object. Since string is immutable.

So in total 4 objects will produce.

Asha said: (Mar 31, 2016)  
How x = x + y; will create object? Can you explain this?

Tamilarasan M said: (Dec 1, 2016)  
Hi All,

Line 1 > "String x = new String("xyz");"
JVM will create 1 object of "xyz" in heap memory, 1 object in string pool for re-use and reference "x" will be created for object in heap memory. /** obj created 2

Line 2>"String y = "abc";"
JVM checks String literal pool for object "abc", if no object is available in String pool, it will create 1 object in String pool /** total object created 2 +1 = 3

Line 3>"x = x+y"
JVM checks String literal pool for object "xyzabc", if no object is available in String pool, it will create 1 object in String pool , now "x" will refer object "xyzabc" in string pool and object "abc" in heap memory will be abandoned and garbage collected /** total object created 3+1=4

So finally we have 1 object "xyz" in heap memory and 3 objects "xyz", "abc", "xyzabc" in string pool.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.