jose chiramal wrote:String x = "abc"
s.concat("def") // this object gets created but gets lost/abandoned , why so ?
System.out.println("x=" + x) // output is x=abc
If you are doing s.concat(), obviously the value of x remains the same, i.e. "abc".
Strings are immutable, they never change. All methods that return a String return a different String object. The original String (s / x) stays the same even if you ignore the result of the concat method.