Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Core java printing statement

Tags:

java

println

core

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)

like image 893
jaga D sh Avatar asked Nov 29 '22 10:11

jaga D sh


1 Answers

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

like image 66
Eran Avatar answered Dec 11 '22 03:12

Eran