如何在Java 8和Java 9中使用unsigned Integer?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了如何在Java 8和Java 9中使用unsigned Integer?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1867字,纯文字阅读大概需要3分钟。
内容图文
![如何在Java 8和Java 9中使用unsigned Integer?](/upload/InfoBanner/zyjiaocheng/714/487393a2ac794ac89bdaf73f50764097.jpg)
在Oracle“原始数据类型”page中,它提到Java 8增加了对无符号整数和长整数的支持:
int
: By default, theint
data type is a 32-bit signed two’s complement integer, which has a minimum value of ?231 and a maximum value of 231?1. In Java SE 8 and later, you can use theint
data type to represent an unsigned 32-bit integer, which has a minimum value of 0 and a maximum value of 232?1. Use theInteger
class to useint
data type as an unsigned integer. See the section The Number Classes for more information. Static methods likecompareUnsigned
,divideUnsigned
etc have been added to theInteger
class to support the arithmetic operations for unsigned integers.
long
: Thelong
data type is a 64-bit two’s complement integer. The signedlong
has a minimum value of ?263 and a maximum value of 263?1. In Java SE 8 and later, you can use thelong
data type to represent an unsigned 64-bitlong
, which has a minimum value of 0 and a maximum value of 264?1. Use this data type when you need a range of values wider than those provided by int. TheLong
class also contains methods likecompareUnsigned
,divideUnsigned
etc to support arithmetic operations for unsignedlong
.
但是,我发现无法声明无符号长整数或整数.例如,下面的代码给出了编译器错误消息“文字超出范围”(我当然使用的是Java 8),当它应该在范围内时(指定的值恰好是264-1):
public class Foo {
static long values = 18446744073709551615L;
public static void main(String[] args){
System.out.println(values);
}
}
那么,有没有办法声明unsigned int或long?
解决方法:
根据您发布的文档和this blog post – 在声明unsigned int / long和signed符号之间的原语时没有区别. “新支持”是在Integer和Long类中添加静态方法,例如Integer.divideUnsigned.如果你没有使用这些方法,那么长于2 ^ 63-1的“无符号”只是一个带有负值的普通旧长.
从快速浏览,看起来没有办法在/ – 2 ^ 31-1或/ – 2 ^ 63-1之外的范围内声明整数常量.您必须手动计算与超出范围的正值相对应的负值.
内容总结
以上是互联网集市为您收集整理的如何在Java 8和Java 9中使用unsigned Integer?全部内容,希望文章能够帮你解决如何在Java 8和Java 9中使用unsigned Integer?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。