简体   繁体   English

Java中的字节缓冲区?

[英]Byte buffer in Java?

Since I found out that it's impossible to have unsigned bytes in java, and that essentially they take up the same memory as an int, is there really any difference when you send them over a packet? 既然我发现在Java中不可能有无符号字节,并且本质上它们与int占用相同的内存,那么当通过数据包发送它们时,真的有区别吗?

If I send a Java byte via tcp or udp via( Games.RealTimeMultiplayer.sendReliableMessage ) would that be more beneficial to me than just sending an integer to represent an unsigned byte? 如果我通过tcp或udp 通过( Games.RealTimeMultiplayer.sendReliableMessage发送Java字节,那么对我来说,比仅发送一个整数来表示一个无符号字节更有益吗?

Since I found out that it's impossible to have unsigned bytes in java 因为我发现在Java中不可能有无符号字节

This is incorrect. 这是不正确的。 There are lots of ways. 有很多方法。 You can even use byte to represent an unsigned byte. 您甚至可以使用byte来表示无符号字节。 You just need to perform a mapping in places that require it; 您只需要在需要它的地方执行映射即可。 eg 例如

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

... and that essentially they take up the same memory as an int. ...并且从本质上讲,它们占用与int相同的内存。

That is also incorrect. 那也是不正确的。 It is true for a byte variable or field. 对于byte变量或字段,则为true。 However, the bytes in a byte array occupy 1/4 of the space of integers in an int array. 但是, byte数组中的byte占据int数组中整数空间的1/4。

... is there really any difference when you send them over a packet? ...通过数据包发送时,真的有什么区别吗?

Well yes. 嗯,是。 A byte sent over the network (in the natural fashion) takes 1/4 of the number of bits as an int sent over the network. 通过网络(自然方式)发送的byte占用通过网络发送的int位数的1/4。 If you are sending an "8 bit quantity" as 32 bits, then you are wasting network bandwidth. 如果要发送32位的“ 8位数量”,那么您正在浪费网络带宽。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM