0

I am Confused as to why this returns 1;

(char)('0' + 11) = ; why?

Full code below where ending = 1;

 char[] ending;
       char a = (char)('0' + 11/10); 
        ending = new char[]{a, (char)('0' + 11)};
        System.out.println(ending);
Mac Tíre
  • 37
  • 8

2 Answers2

2
  • Char value of '0' is 48.
  • 48 + 11 = 59
  • Char value of 59 is ';'.

You can check char values in integer value in any ASCII Character Set in internet.

In Java, char can be use as a int, short, byte, long with values between 0 and 65535 without any casting.

A better explanation is found in: Java char is also an int?

Kosonome
  • 470
  • 2
  • 9
  • 1
    Internally java chars are 2 bytes so more like shorts than integers. – cquezel Mar 03 '20 at 17:37
  • 1
    Java characters are UTF-16 encodings of Unicode characters. You can look into a unicode table for characters that are up to 15 significant bits. – cquezel Mar 03 '20 at 17:46
1

You are assigning '1' to variable a. '0' + 11/10 => '0' + 1 You are assigning a two letter string to endings. Le first letter is a ('1') the second is a semi colon. ('0' + 11).

cquezel
  • 3,859
  • 1
  • 30
  • 32