Why doesn’t my compare work between char and int in Java?

Although this question is very unclear, I am pretty sure the poster wants to know why this prints false:

char c="0";
int i = 0;
System.out.println(c == i);

The answer is because every printable character is assigned a unique code number, and that’s the value that a char has when treated as an int. The code number for the character 0 is decimal 48, and obviously 48 is not equal to 0.

Why aren’t the character codes for the digits equal to the digits themselves? Mostly because the first few codes, especially 0, are too special to be used for such a mundane purpose.

Leave a Comment