Are there any real life uses for the Java byte primitive type?

后端 未结 10 1728
清歌不尽
清歌不尽 2020-12-23 13:16

For some inexplicable reason the byte primitive type is signed in Java. This mean that valid values are -128..127 instead of the usual 0..255 range representin

10条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-23 13:49

    byte, short, char types are mostly useless, except when used in arrays to save space.

    Neither Java or JVM has any real support for them. Almost all operations on them will promote them to int or long first. We cannot even write something like

    short a=1, b=2;
    a = a + b;  // illegal
    a = a << 1; // illegal
    

    Then why the heck even bother with defining operations on byte, short, char types at all?

    All they do are sneaking in widening conversions that will surprise the programmer.

提交回复
热议问题