saurav
saurav

Reputation: 422

Increment operator on String in Groovy

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

Answers (1)

steve v
steve v

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

Related Questions