jaga D sh jaga D sh - 1 month ago 20
Java Question

Core java printing statement

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)

Answer

'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.

Comments