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?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.
Deepak said:
1 decade ago
"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.
xyz will be lost as x now points to new concatenated string. Hence
5-1 = 4 objects.
Md Zakir said:
1 decade ago
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.
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:
1 decade ago
Please summarize somebody, can we fix the answer as 3 objects.
Please conclude the discussion with proper explanation.
Thanks.
Please conclude the discussion with proper explanation.
Thanks.
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.
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.
Jinesh said:
1 decade ago
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.
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:
1 decade ago
I agree that Line 1 creates 2 object, and 2nd line 1 object but how 4th object is created?
Xyzabc said:
1 decade ago
@Anandi.
String is immutable so new object xyzabc is created.
String is immutable so new object xyzabc is created.
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.
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:
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.
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.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers