5

I'm looking for an explanation for Java's behavior when handling the following scenarios. I understand that the ASCII table is arranged so that the value of the character 5 is five positions greater than 0. This allows for calculations to be done on the char without converting to an int as seen in the first example.

What I don't understand is why Java seems to inconsistently handle when to provide a value from an ASCII table and when to do a calculation on the chars as though they were integers.

int x = '5' - '0';

output x = 5;

int x = '5'

output x = 53;

Now for some examples, that introduce confusion.

int x = '0' + 1 - '5'

output x = -4

int y = '5' - '0' + '1'

output 54

int y = '5' - 0 + '1'

output 102

Java seems to be doing an implicit type conversion, but how is Java inferring which representation of the int/char should it be using?

5
  • 5
    All these seem consistent. If you translate the character to their codes, the math makes sense. Commented Jan 28, 2019 at 13:03
  • The difference between '0' and '5' is 5, but '5' has a numeric value of 53. char are an integer type, but they do not correspond to their character representation. Commented Jan 28, 2019 at 13:06
  • 1
    Note that in all of these examples, the chars are converted to int before performing the operations. Commented Jan 28, 2019 at 13:07
  • 1
    You should be aware that char is a UTF-16 code unit, not ASCII. Java doesn't use ASCII (similarly for JavaScript, .NET, VB4/5/6/A/Script, SQL NCHAR NVARCHAR,…). UTF-16 is one of several character encoding for the Unicode character set. Commented Jan 28, 2019 at 20:25
  • Thanks for that, the first 128 characters of Unicode are identical to ASCII. Commented Jan 28, 2019 at 23:14

5 Answers 5

5

Just write the char conversion to ASCII code (below your statements)

int x = '0' + 1 - '5'
        48  + 1 - 53 = -4 

int y = '5' - 0 + '1'
         53 - 0 + 49 = 102

int y = '5' - '0' + '1'
         53 - 48  + 49 = 54

Notice it's consistent, each int remains int and each char converted to ASCII code

Sign up to request clarification or add additional context in comments.

Comments

1

This might seem to be inconsistent but in real they are consistent.

int x = '5' - '0';

output x = 5; because behind the back ASCII codes are, '5'=53 and '0'=48. Hence

int x = '5'

output x = 53;

Comments

1

You might be mixing the representation from the value. The values never change, so when you perform arithmatic it will always be that '5'==53 and not 5. For the display JLS on primitive to string conversion.

Integer arithmetic is promoted to int for most calculations.

System.out.println('5' + '0');
>>> 101

System.out.println((char)('5' + '0'));
>>> e

Both results have the same numeric value, but one is displayed as a character because it has been cast to character.

Comments

1

Java seems to be doing an implicit type conversion, but how is Java inferring which representation of the int/char should it be using?

It's actually quite simple. char is one of the numeric types in Java, see 4.2.1. Integral Types and Values:

The values of the integral types are integers in the following ranges:

  • [...]
  • For char, from '\u0000' to '\uffff' inclusive, that is, from 0 to 65535

All operations on integer types are carried out either with int- or long- precision, see 4.2.2. Integer Operations:

If an integer operator other than a shift operator has at least one operand of type long, then the operation is carried out using 64-bit precision, and the result of the numerical operator is of type long. If the other operand is not long, it is first widened (§5.1.5) to type long by numeric promotion (§5.6).

Otherwise, the operation is carried out using 32-bit precision, and the result of the numerical operator is of type int. If either operand is not an int, it is first widened to type int by numeric promotion.

Note the last sentence: this defines the conversion to be applied, it is called "numeric promotion".

Comments

1

char '0' does not equals int 0. char '0''s binary representation occupies 16 bit:

0000 0000 0011 0000

while int 0's binary representation occupies 32 bit:

0000 0000 0000 0000 0000 0000 0000 0000 

When you sum a char and an int, the char will be promoted to int first.

For example. char 5's unicode is 0035, in binary 0000 0000 0011 0101, it will be promoted to int by inserting 16 zeros at head, 0000 0000 0000 0000 0000 0000 0011 0101, and the int represents 53 in decimal.

4 Comments

Do you mean '0' is not equal to 0, becausechar c = 0; c is zero, but char c = '0'; gives you the value you claim.
They are in different types.
What are different types? You can have a char with a value of zero, ideone.com/s5DPpJ .
char c = '0' and int b = 0. I mean.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.