Reputation: 45
Though we all know that Java doesn't support operator overloading, then why is the +
operator an arithmetic operator as well as String
concatenation operator.
Can anybody explain this?
Upvotes: 0
Views: 86
Reputation: 5837
When we use +
with strings, compiler actually convert them to use StringBuilder
.
Upvotes: 0
Reputation: 200176
Java doesn't allow custom operator overloading. Several operators, not just +
, are overloaded by specification, and that's the way they stay.
The main issue with custom operator overloading is the opaqueness and unpredictability of their semantics, contributing to the probability of massive WTF moments while reading (and even writing) code.
Upvotes: 7