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

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.

Kumari Akankshya Punji said:   7 years ago
As far as my knowledge, an object is only created when the 'new' keyword is used. If not it is termed as a reference. There is a whole lot of difference between reference and an object. Here instantiation has only been done once. So the answer according to me is 1.
(1)

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.

Aslam anwer said:   6 years ago
String x = "xyz";
String why = "abc";
String a = x + y;
String b = x + y;

System.out.println (a == b) ; // prints false.

Plus operator with variables will always create new object.

So in the above example it creates 4 objects.
(2)

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 ?

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.

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 !

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.

Istvan Bohm said:   10 years ago
String x = new String("xyz");

First: The argumentum, "xyz".

Second: We create a new string with the copy constructor of the String class from the argumentum.

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.


Post your comments here:

Your comments will be displayed after verification.