Operator OverLoading Code
I am new to Java. if this question is simple one then don't scoled me.
operator oveloading is there or not in java?
if it is not there then why the following code is behaving differently?
System.out.println(1+2); (adding)
System.out.println("Hai"+1+2); (concatinating)
System.out.println(1+2+"Hai"+1+2); (first adding & then concatinating)
if operator overloading is there then please explain.
Re: Operator OverLoading Code
[QUOTE=vinodjallapally;17292]I am new to Java. if this question is simple one then don't scoled me.
operator oveloading is there or not in java?
if it is not there then why the following code is behaving differently?
System.out.println(1+2); (adding)
System.out.println("Hai"+1+2); (concatinating)
System.out.println(1+2+"Hai"+1+2); (first adding & then concatinating)
if operator overloading is there then please explain.[/QUOTE]
-----
hi
this follows a simple rule of '+' operation of java.
The rule is " if either operand is string then it converted the number value to stirng and concates with the string value, addtion only occurs only when both the operands are number".
1)System.out.println(1+2); (adding)
Reason: both the operands are number
2) System.out.println("Hai"+1+2); (concatinating)
+ is haveing left associativy so the output will be "Hai12"
Execution order is as below:
"Hai"+ 1 = "Hai1" then
"Hai1"+2 = "Hai12"
3) System.out.println(1+2+"Hai"+1+2); (first adding & then concatinating)
Execution order:
1+2 = 3 (both are number)
3 + "Hai" = "3Hai"
"3Hai"+1 = "3Hai1"
"3Hai1" + 2 = "3Hai12"
i think this will clear your doubts :)