Incrementing Char Type In Java

前端 未结 10 1593
鱼传尺愫
鱼传尺愫 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:30

    char is a numeric type (2 bytes long), and is also the only unsigned numeric primitive type in Java.

    You can also do:

    int foo = 'A';
    

    What is special here is that you initialize the char with a character constant instead of a number. What is also special about it is its string representation, as you could witness. You can use Character.digit(c, 10) to get its numeric value (as an int, since 2 ^ 16 - 1 is not representable by a short!).

提交回复
热议问题