Incrementing Char Type In Java

前端 未结 10 1600
鱼传尺愫
鱼传尺愫 2020-12-03 05:08

While practicing Java I randomly came up with this:

class test
{
    public static void main(String arg[])
    {
        char x=\'A\';
        x=x+1;
                


        
10条回答
  •  南笙
    南笙 (楼主)
    2020-12-03 05:26

    A char is in fact mapped to an int, look at the Ascii Table.

    For example: a capital A corresponds to the decimal number 65. When you are adding 1 to that char, you basicly increment the decimal number by 1. So the number becomes 66, which corresponds to the capital B.

提交回复
热议问题