[英]Unsigned int (primitive) and Integer (Object) usage in Java
I'm following the Java tutorial on Primitive Data Types . 我正在关注原始数据类型的Java教程 。 Early on, it states that
早期,它说明了这一点
In Java SE 8 and later, you can use the int data type to represent an unsigned 32-bit integer, which has a minimum value of 0 and a maximum value of 2^32-1.
在Java SE 8及更高版本中,您可以使用int数据类型来表示无符号的32位整数,其最小值为0,最大值为2 ^ 32-1。 Use the Integer class to use int data type as an unsigned integer.
使用Integer类将int数据类型用作无符号整数。
What I'm understanding from this quote is that I can now store up to 2^32-1 as an int as long as I use the Integer wrapper object rather than the int primitive data type. 我从这句话中理解的是,只要我使用Integer包装器对象而不是int原始数据类型,我现在可以存储最多2 ^ 32-1作为int。 But when I tried this out, my compiler is complaining that the value I'm using is too large, 2^31.
但是当我尝试这个时,我的编译器抱怨我使用的值太大,2 ^ 31。 I've tried this using both the primitive data type and Object.
我已经尝试使用原始数据类型和Object。
Integer integerObjectLarge = 2147483648; //2^31
int integerPrimitiveLarge = 2147483648; //2^31
How exactly do I use an int/Integer to store unsigned value, such as 2^31? 我究竟如何使用int / Integer存储无符号值,例如2 ^ 31?
I don't think Java has added a true unsigned 32-bit int
type. 我认为Java没有添加真正的无符号32位
int
类型。 The only thing that's changed is that the Integer
class now contains some methods that will treat a 32-bit integer(s) as an unsigned 32-bit integer. 唯一改变的是
Integer
类现在包含一些将32位整数视为无符号32位整数的方法。 See compareUnsigned
, divideUnsigned
, parseUnsignedInt
, and several others in the javadoc for Integer
. 有关
Integer
的javadoc,请参阅compareUnsigned
, divideUnsigned
, parseUnsignedInt
和其他几个。 Long
has some new methods too. Long
也有一些新方法。
But basically, if you store the bit pattern (for example) 10001000_11110000_10010110_11000010
in an int
, there's nothing in the int
that says "This is an unsigned integer" or "This is a signed integer". 但基本上,如果将位模式(例如)
10001000_11110000_10010110_11000010
在int
,则int
中没有任何内容表示“这是无符号整数”或“这是有符号整数”。 It depends on what methods you use to work with it. 这取决于您使用哪种方法来处理它。
But when I tried this out, my compiler is complaining that the value I'm using is too large, 2^31.
但是当我尝试这个时,我的编译器抱怨我使用的值太大,2 ^ 31。
You have an error because the literal 2147483648
is syntactically invalid. 您有一个错误,因为文字
2147483648
在语法上是无效的。
How exactly do I use an int/Integer to store unsigned value, such as 2^31?
我究竟如何使用int / Integer存储无符号值,例如2 ^ 31?
You can still perform unsigned arithmetic using the new methods of the Integer
class, just don't use invalid literals: 您仍然可以使用
Integer
类的新方法执行无符号算术,只是不要使用无效的文字:
int n = 2147483647 + 1; // 2^31
System.out.println(Integer.toUnsignedString(n)); // treat int as unsigned
System.out.println(Integer.toString(n)); // treat int as signed
2147483648 -2147483648
What I'm understanding from this quote is that I can now store up to 2^32-1 as an int as long as I use the Integer wrapper object rather than the int primitive data type.
我从这句话中理解的是,只要我使用Integer包装器对象而不是int原始数据类型,我现在可以存储最多2 ^ 32-1作为int。
You can store the unsigned int using the primitive type int
, but using the value as an unsigned type requires you to use the utility methods of Integer
, as can be seen above. 您可以使用基本类型
int
存储 unsigned int,但是将该值用作无符号类型需要您使用Integer
的实用方法,如上所示。
You've always been able to store "unsigned" integer literals by using hex: 您始终可以使用十六进制存储“无符号”整数文字:
int x = 0xffffffff;
// or any value >= 0x80000000;
This is because, for all practical purposes, the compiler thinks these are "signed". 这是因为,出于所有实际目的,编译器认为这些是“签名”的。
You still can't assign a decimal value > 2^31 - 1 because it would create all sorts of backwards compatibility issues if this were suddenly allowed. 您仍然无法指定小数值> 2 ^ 31 - 1,因为如果突然允许这会产生各种向后兼容性问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.