Java does not have concept of operator overloading.
Still + operator behaves as addition operator with numbers and concatenate operator with strings. This is similar to the operator overloading behavior.
So, does Java have operator overloading?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Does the Java language overload some operators?
YES! As you’ve found out, the operator
+can mean two different things, string concatenation or numeric addition. This is, by definition, an operator overload.Here’s the list of all Java operators:
Some of those operators are overloaded. Here are some examples:
Can we overload the operators defined in the Java language?
ABSOLUTELY NOT! All Java operators mean exactly as specified by the language specification. There is no "extra-linguistic" semantics: a Java operator can NEVER do something that isn’t specified by the language.
That is, unless the language changes, the following are guaranteed truths:
someString + whateveris ALWAYS string concatenationreferenceType == anotherReferenceTypeis ALWAYS reference equality3 * "a lady"or"my heart" / 2or even10**3 ~= 999As the above snippet shows, however, even the current state of operator overloading can still be quite confusing, especially for beginners. By not allowing extra-linguistic overloads, at least this confusion is limited: once a programmer learns about what all the operators in the Java language do in various overloaded scenarios, their exact semantics in all Java code becomes clear and precise.
Operator overloading can be quite confusing. Some think that it’s "bad" enough as it is. To allow users to overload the Java operators to do something outside the language specification can only lead to even more confusion.
Here’s an excerpt from Java Puzzlers, Puzzle 30: Son of Looper:
Do you need C++ to support operator overloading in Java?
NOPE! This has nothing to do with it at all. All that the Java compiler needs to do is parse the program source code according to the grammatical rules of the language, and determine, for each operator, what the types of the operands are. This information is enough to deduce what the meaning of the operator is, and to then act accordingly as specified by the language.
Appendix
JLS References
Revealing questions