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 1 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)

Nira said:   9 years ago
Hi all friends.

The line => "String x = new String("xyz");" will produce two objects. One in the heap area and another one in String constant pool.

And the line => "String y = "abc";" will produce only one in the String constant pool.

Again line =>"x = x + y;" will produce one more object. Since string is immutable.

So in total 4 objects will produce.
(2)

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)

Tamilarasan M said:   9 years ago
Hi All,

Line 1 > "String x = new String("xyz");"
JVM will create 1 object of "xyz" in heap memory, 1 object in string pool for re-use and reference "x" will be created for object in heap memory. /** obj created 2

Line 2>"String y = "abc";"
JVM checks String literal pool for object "abc", if no object is available in String pool, it will create 1 object in String pool /** total object created 2 +1 = 3

Line 3>"x = x+y"
JVM checks String literal pool for object "xyzabc", if no object is available in String pool, it will create 1 object in String pool , now "x" will refer object "xyzabc" in string pool and object "abc" in heap memory will be abandoned and garbage collected /** total object created 3+1=4

So finally we have 1 object "xyz" in heap memory and 3 objects "xyz", "abc", "xyzabc" in string pool.
(1)

Sopan Bagalkar said:   10 years ago
Simple explanation:

String x = new String("xyz");

if (x=="xyz") {
System.out.println("true");
}
else {
System.out.println("false");
}

String y = "abc";
x = x + y;
System.out.println(x);


Output:

False.
xyzabc.
(1)

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.

Raghavendra said:   6 years ago
Heap/scp
x-xyz . xyz.
x-xyzabc y-abc

4 objects are created.

Asha said:   9 years ago
How x = x + y; will create object? Can you explain this?

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.

Bablu said:   10 years ago
Given this line of code: String s = new String("xyz").

There are two ways of looking at this:

(1) What happens when the line of code executes -- the literal moment it runs in the program?

(2) What is the net effect of how many Objects are created by the statement?

Answer:

1) After this executes, one additional object is created.
a) The "xyz" String is created and interned when the JVM loads the class that this line of code is contained in. If an "xyz" is already in the intern pool from some other code, then the literal might produce no new String object.

b) When new String s is created, the internal char[] is a copy of the interned "xyz" string.

c) That means, when the line executes, there is only one additional object created.

The fact is the "xyz" object will have been created as soon as the class loaded and before this code section was ever run.

Next scenario:

2) There are three objects created by the code (including the interned "a")
String s1 = "a";
String s2 = "a";
String s3 = new String("a");

a) s1 and s2 are just referenced,not objects, and they point to the same String in memory.

b) The "a" is interned and is a compound object: one char[] object and the String object itself. It consisting of two objects in memory.

c) s3, new String("a") produces one more object. The new String("a") does not copy the char[] of "a", it only references it internally. Here is the method signature:

public String2(String original) {
this.value = original.value;
this.hash = original.hash;
}

One interned String ("a") equals 2 Objects. And one new String("a") equals one more object. Net effect from code is three objects.


Post your comments here:

Your comments will be displayed after verification.