Java 中的字节缓冲区?

Byte buffer in Java?

因为我发现 java 中不可能有无符号字节,而且基本上它们占用与 int 相同的内存,所以当你通过数据包发送它们时真的有什么区别吗?

如果我通过 tcp 或 udp via(Games.RealTimeMultiplayer.sendReliableMessage) 发送一个 Java 字节,这对我来说比只发送一个整数更有益吗代表一个无符号字节?

Since I found out that it's impossible to have unsigned bytes in java

这是不正确的。有很多方法。您甚至可以使用 byte 来表示无符号字节。你只需要在需要它的地方进行映射;例如

    byte b = ...
    int unsigned = (b >= 0) ? b : (b + 256);

... and that essentially they take up the same memory as an int.

这也是不正确的。 byte 变量或字段也是如此。但是,byte 数组中的字节占 int 数组中 space 整数的 1/4。

... is there really any difference when you send them over a packet?

嗯,是的。通过网络发送的 byte(以自然方式)占用的位数是通过网络发送的 int 的位数的 1/4。如果您将“8 位数量”作为 32 位发送,那么您就是在浪费网络带宽。