The method Concat() does not modify the original value. It returns a new value.
like this:
String str = 'good'; str.concat('ness'); System.out.println(str); //'good'
But some method modify the original value. Why?
In Groovy:
def languages = ['Java', 'Groovy', 'JRuby'] languages.reverse() ===> [JRuby, Groovy, Java] println languages ===> [Java, Groovy, JRuby] languages.sort() ===> [Groovy, JRuby, Java] println languages ===> [Groovy, JRuby, Java]
Stringis immutable in Java. Any method that ‘modifies’ aStringmust return a new instance ofString.From the Java API Specifications for the
Stringclass:The Java Language Specifications defines this behavior in Section 4.3.3: The Class String.
Response to the edit:
It appears that an example in Groovy has been added. (I haven’t used Groovy before, so my understanding of it may not be correct.)
From what I understand from looking at the example, there seems to be a
languageslist that is beingreverse-ed andsort-ed — those operations themselves do not modify theStringobjects contained in the list, but are acting upon the list itself.The way the list is returns a new list, or how it modifies or doesn’t modify the list is not related to the behavior of the
Stringobjects themselves.