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 3 of 4.

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

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.

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.

Suhas said:   1 decade ago
I am not able to understand the concept behind the answer. Kindly exlpain the solution in detail.

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?

Ramesh said:   1 decade ago
Only 3 objects are created the first line creates only one object.

Xyzabc said:   1 decade ago
@Anandi.

String is immutable so new object xyzabc is created.

Raghavendra said:   6 years ago
Heap/scp
x-xyz . xyz.
x-xyzabc y-abc

4 objects are created.

Asha said:   9 years ago
How x = x + y; will create object? Can you explain this?


Post your comments here:

Your comments will be displayed after verification.