Reputation: 422
s = 'S'
s++
println(s)
Since s being a string is Immutable, How come the output is T for the above lines of code?
Does it interpret s++ as s = s.next()?
Upvotes: 1
Views: 8707
Reputation: 3540
Does it interpret s++ as s = s.next()
Yes. To overload operators in groovy you implement specifically named methods, per the language documentation
All (non-comparator) Groovy operators have a corresponding method that you can implement in your own classes. The only requirements are that your method is public, has the correct name, and has the correct number of arguments.
The method for ++
is next()
Upvotes: 7