Adding char and int

前端 未结 7 2056
梦如初夏
梦如初夏 2020-11-30 10:51

To my understanding a char is a single character, that is a letter, a digit, a punctuation mark, a tab, a space or something similar. And therefore when I do:

7条回答
  •  温柔的废话
    2020-11-30 11:29

    '1' is a digit, not a number, and is encoded in ASCII to be of value 49.

    Chars in Java can be promoted to int, so if you ask to add an int like 1 to a char like '1', alias 49, the more narrow type char is promoted to int, getting 49, + 1 => 50.

    Note that every non-digit char can be added the same way:

    'a' + 0 = 97
    'A' + 0 = 65
    ' ' + 0 = 32
    

提交回复
热议问题