s.concat("def")
returns a new String
instance. The String
instance referenced by s
remains unchanged.
You can add another println
statement to see that for yourself :
public static void main(String[] args) {
String s = new String("abc");
System.out.println(s.concat("def"));
System.out.println(s);
}
You can also see that in the Javadoc of concat
:
If the length of the argument string is 0, then this String object is returned. Otherwise, a String object is returned that represents a character sequence that is the concatenation of the character sequence represented by this String object and the character sequence represented by the argument string.
The original String
is returned only if you pass an empty String
to this method.