Why does java compiler gives
100a
as output when I ever tried to print System.out.println('2'+'2'+"a")
and
a22
for System.out.println("a"+'2'+'2')
. Please explain in detail . thank you)
'2' is a char, so '2' + '2' adds the int value of that character to itself (50+50) and then appends "a" to it, giving you 100a.
"a" + '2' + '2' performs String concatenation, since the first operand is a String. Therefore you get a22.
Note that the expressions are evaluated from left to right, so the types of the first two operands determine whether + will perform an int addition or a String concatenation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With