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

后端 未结 10 1647
清歌不尽
清歌不尽 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 14:09

    Digitized sound (or any other signal) with 8 bit signed samples seems like the only reasonable example to me. Of course having signed bytes is no requirement to handling such signals and it can be argued whether Java byte "fits perfectly".

    Personally I think not having unsigned is a mistake. Not only because there's more use for unsigned bytes/ints but because I prefer a stronger type system. It would be nice to be able to specify that negative numbers are not valid and allow compiler checks and runtime exceptions for violations.

提交回复
热议问题