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.
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. :).
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.
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.
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.
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?
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.
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.
Please conclude the discussion with proper explanation.
Thanks.
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.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers