在java中处理无符号数据的公认方法是什么
我读到(并且目前正在使用这个)只要我需要一个无符号整数,我就可以使用 long 。这对我来说效果很好,但感觉设计很糟糕。有没有更好、更容易被接受的方法呢?那么无符号长整数呢?
I read (and am currently using this) that I can use a long whenever I need an unsigned int. This is working fine for me, but feels like a poor design. Is there a better, more accepted, way to do it? What about unsigned longs?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
由于 Java 中唯一的无符号基元类型是
char
,它具有非常特定的含义来表示 unicode 字符,因此使用long
是最惯用的方法。默认情况下,JAXB 也将
xsd:unsignedInt
映射到long
。 (来源)编辑:如果您知道最上面的一点为 0,或者仅使用符号无关紧要的操作(例如,您只是传递数据),
int
可能更常见。 JAXB 仅默认为long
,因为它无法知道您打算如何使用数据。Since the only unsigned primitive type in Java is
char
, which has a very specific meaning to denote a unicode character, usinglong
is the most idiomatic way to go.By default, JAXB maps
xsd:unsignedInt
tolong
, too. (source)Edit: If you know the top bit to be 0, or only use operations where the sign doesn't matter (for instance, you're just passing data through),
int
is probably more common. JAXB only defaults tolong
because it can't know how you intend to use the data.Java 没有选择无符号类型,您使用比原始无符号类型大的有符号类型来解决限制,但因此您使用了所需内存的两倍。
在java中没有无符号的概念。
Java doesn't have unsigned types by choice, you are using the signed types that are larger than the original unsigned type to work around the limitation but so you are using twice of the needed memory.
In java there is no concept of unsigned .