What will be the output of following quiz on String concatenation ?
Answer
Here are some rules for this :
1) When one of the operands in + is string, its concatenated. The result is a String.
2) When both operands are numbers, numbers are added.
3) The operation is evaluated from left to right.
We have,
int one =1;
String two = “2”;
Now,
two + 1 + one
= (“2” + 1) + 1 //parenthesis used to denote left to right evaluation
= “21” + 1
= “211”
Similarly,
one + 1 + two
= (1 + 1) + “2” //left to right evaluation
=”22″
So, answer is 21122
Read more here:
Java + operator and Operator Overloading
© 2016, https:. All rights reserved. On republishing this post, you must provide link to original post