Originally posted by Raj Kumar Bindal:
+ operator is overloaded for strings in java.But,i am not clear where it is defined and how the operator overloading happens internally.Please tell me.
If you mean what rule is applied by the compiler to decide whether to use arithmetic addition or string concatenation, it is like this:
If one or both operands are a string, + means string concatenation, else + means addition.
So, if you write
"Hello" + 1 + 2, this evaluates to
"Hello12" because the operators are evaluated left to right. If you change the order of evaluation with parentheses and write
"Hello" + (1 + 2), the rightmost + is evaluated first, and interpreted as an addition because both operands are int literals, so the expression evaluates to
"Hello3".
[ January 14, 2007: Message edited by: Enrico Savazzi ]