Line 1: String x = "Java";
Line 2: x.concat(" Rules!");
Line 3: System.out.println("x = " + x);
Output is "x= Java"
Line 1:creates a new String object, gives the value "Java", and refer x to it.
Line 2: VM creates a 2nd String object with value "Java Rules!" but nothing refers to it. THE 2nd STRING OBJECT IS INSTANTLY LOST; YOU CANNOT GET TO IT.
As these String Objects are created in Heap, will the 2nd object be Garbage Collected.
Enosh, in java Strings are immutable, so you should assign
x = x.concat(" Rules");
for the second line and then it will work.
The second object will be GC'd eventually because there is no longer an entity refering to it.
Absolutely. That's the whole point of garbage collection.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With