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 1 of 4.
Afin said:
1 decade ago
Line 1 creates only 1 reference which will point xyz.
Sanjay Singh said:
1 decade ago
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.
"Line 1 creates two, one referred to by x and the lost String "xyz"----------Please ellaborate.
Mmintz01 said:
1 decade ago
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 !
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:
1 decade ago
@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 ?
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:
1 decade ago
I am not able to understand the concept behind the answer. Kindly exlpain the solution in detail.
Sonam said:
1 decade ago
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.
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:
1 decade ago
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.
So only 2 objects are created in all.
Please correct me if I'm wrong.
Ramesh said:
1 decade ago
Only 3 objects are created the first line creates only one object.
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;.
Harikrishna M said:
1 decade ago
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.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers