Can the char type be categorized as an integer?

前端 未结 8 2450
盖世英雄少女心
盖世英雄少女心 2020-12-01 22:32

Just now I read \"char is the only unsigned integral primitive type in Java.\" Does this mean the char is one of the integral types in Java?

Same as in C, recently I

相关标签:
8条回答
  • 2020-12-01 23:23

    I'm unsure of the formal definition of an integral type, but in short, yes, char is an integral type in Java, since it can be seen as representing an integer.

    You can for instance do

    char c1 = 'a' + 'b';
    
    char c2 = 5;
    
    char c3 = c2 + 3;
    
    int i = c3;
    char c4 = (char) i;
    

    and so on.

    0 讨论(0)
  • 2020-12-01 23:26

    The C specification calls for the char type to be implemented as a 1-byte integer. The specifications for other types are not always so clear. For example, the original Kernigan and Ritchie C book said that, as far as short and long were concerned, among different implementations, you could only rely on the fact that short was no longer than long.

    0 讨论(0)
提交回复
热议问题