Java:无符号数字 [英] Java: Unsigned numbers

查看:33
本文介绍了Java:无符号数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Java 中有没有办法像在 (My)SQL 中那样使用无符号数?

Is there a way in Java to use unsigned numbers like in (My)SQL?

例如:我想使用一个 8 位变量(byte),范围类似于:0 ... 256;而不是 -128 ... 127.

For example: I want to use an 8-bit variable (byte) with a range like: 0 ... 256; instead of -128 ... 127.

推荐答案

不,Java 除了 char(有效值为 0-65535)之外没有任何无符号原始类型.这很痛苦(特别是对于 byte),但事情就是这样.

No, Java doesn't have any unsigned primitive types apart from char (which has values 0-65535, effectively). It's a pain (particularly for byte), but that's the way it is.

通常你要么坚持相同的大小,并为高"数字溢出到负数,要么使用更宽的类型(例如 short 用于 byte)并应对具有额外的内存要求.

Usually you either stick with the same size, and overflow into negatives for the "high" numbers, or use the wider type (e.g. short for byte) and cope with the extra memory requirements.

这篇关于Java:无符号数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆