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 3 of 4.
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.
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.
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.
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;.
Ramesh said:
1 decade ago
Only 3 objects are created the first line creates only one object.
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.
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.
Suhas said:
1 decade ago
I am not able to understand the concept behind the answer. Kindly exlpain the solution in detail.
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 ?
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 !
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers