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

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)


Post your comments here:

Your comments will be displayed after verification.