How can an integer and a symbol be in the same mathematical expression?

I was studying a book and I found this:

int tiles = ch - 'A';

where chis the symbol.

What I don't understand is how the work between two characters leads to an integer?

+4
source share
3 answers

The variable A is charactually stored in memory as a numeric value. This number is a specific symbol ( a, b, é, ...) in accordance with a particular codification (typically ASCII Table ).

For example:

Decimal    Char
----------------
65         A
66         B
...        ...

char, . , char. , char.

// The ASCII code for 65 is A
int result = 100 - 'A';
System.out.println ("100 - A is " + result); // Prints 35

System.out.println("ASCII code for 65: " + (char)65); // Prints A

.

, JLS, 5.6 0,2. 5.1.2. :

19 :

  • ...
  • char int, long, float double
  • ...
+6

:

char: char - 16- . '\ u0000' ( 0) '\ uffff' ( 65 535 ).

, , char . ,

char,

, oracle docs , char int ,

  • byte , int, long, float double
  • short int, long, float double
  • char int, long, float double
  • int long, float double
  • float

:

Java char int ( ) .

Java : a char int ( ASCII ). , 'A' + 1, char 'A' int 65, int 1, , int 66. , 1.5 + 'A', 'A' int 65, 65. 66.5.

, '5' - '0' char int (53 48 : . ASCII), : int 5. , char d = '8'; ( , ), d-'0 ' , int ( 8). , ASCII "8" 56, "0" 48.

+3

ascii ( ). , , . "ch - A" 0 26, "ch" A Z.

+1

Source: https://habr.com/ru/post/1525076/


All Articles