sanjitguin
sanjitguin

Reputation: 45

Operator overloading confusion in Java

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

Answers (2)

RP-
RP-

Reputation: 5837

When we use + with strings, compiler actually convert them to use StringBuilder.

Upvotes: 0

Marko Topolnik
Marko Topolnik

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

Related Questions