Java Programming - Java.lang Class - Discussion

Discussion Forum : Java.lang Class - Finding the output (Q.No. 1)
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?
2
3
4
5
Answer: Option
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".

Discussion:
31 comments Page 2 of 4.

Pankaj Rana said:   1 decade ago
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).

JavaDeveloper said:   1 decade ago
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. :).

Richa said:   1 decade ago
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.

Harsha rao said:   1 decade ago
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.

Xyzabc said:   1 decade ago
@Anandi.

String is immutable so new object xyzabc is created.

Anandi Shewale said:   1 decade ago
I agree that Line 1 creates 2 object, and 2nd line 1 object but how 4th object is created?

Afin said:   1 decade ago
Line 1 creates only 1 reference which will point xyz.

Gaurav Kumar said:   1 decade ago
Can anyone tell me that why line one creates two object. It should create only one object and this answer is wrong.

M pradeep said:   1 decade ago
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.

Dasoju said:   1 decade ago
Please summarize somebody, can we fix the answer as 3 objects.

Please conclude the discussion with proper explanation.

Thanks.


Post your comments here:

Your comments will be displayed after verification.