Java int 总是 32 位吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1032982/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is a Java int always 32 bits?
提问by
Will Java's int
always and everywhere be a 32 bit signed integer?
Javaint
总是和任何地方都是 32 位有符号整数吗?
回答by coobird
Yes, it's defined in The Java Language Specification.
是的,它在The Java Language Specification 中定义。
From Section 4.2: Primitive Types and Values:
The integral types are
byte
,short
,int
, andlong
, whose values are 8-bit, 16-bit, 32-bit and 64-bit signed two's-complement integers, respectively, andchar
, whose values are 16-bit unsigned integers representing UTF-16 code units (§3.1).
整数类型为
byte
、short
、int
和long
,其值分别为 8 位、16 位、32 位和 64 位有符号二进制补码整数,和char
,其值为表示 UTF-16 代码的 16 位无符号整数单位(第 3.1 节)。
And additionally from Section 4.2.1: Integral Types and Values:
另外来自第 4.2.1 节:积分类型和值:
The values of the integral types are integers in the following ranges:
- For byte, from -128 to 127, inclusive
- For short, from -32768 to 32767, inclusive
- For int, from -2147483648 to 2147483647, inclusive
- For long, from -9223372036854775808 to 9223372036854775807, inclusive
- For char, from '\u0000' to '\uffff' inclusive, that is, from 0 to 65535
整数类型的值是以下范围内的整数:
- 对于字节,从 -128 到 127,包括
- 简而言之,从-32768到32767,包括
- 对于 int,从 -2147483648 到 2147483647(含)
- 长期,从-9223372036854775808到9223372036854775807,含
- 对于char,从'\u0000'到'\uffff'包含,即0到65535
回答by drRoflol
int
s are 32 bits. Should you need more, long
s are 64 bits.
int
s 是 32 位。如果您需要更多,long
s 是 64 位。
回答by roblovelock
Java 8 has added some support for unsigned integers.The primitive int
is still signed, however some methods will interpret them as unsigned.
Java 8 增加了对无符号整数的一些支持。原语int
仍然是有符号的,但是有些方法会将它们解释为无符号。
The following methods were added to the Integer classin Java 8:
Java 8 中的Integer 类添加了以下方法:
- compareUnsigned(int x, int y)
- divideUnsigned(int dividend, int divisor)
- parseUnsignedInt(String s)
- parseUnsignedInt(String s, int radix)
- remainderUnsigned(int dividend, int divisor)
- toUnsignedLong(int x)
- toUnsignedString(int i)
- toUnsignedString(int i, int radix)
- 比较无符号(int x,int y)
- 除无符号(整数被除数,整数除数)
- parseUnsignedInt(String s)
- parseUnsignedInt(String s, int radix)
- 剩余无符号(整数除数,整数除数)
- toUnsignedLong(int x)
- toUnsignedString(int i)
- toUnsignedString(int i, int radix)
Here is an example usage:
这是一个示例用法:
public static void main(String[] args) {
int uint = Integer.parseUnsignedInt("4294967295");
System.out.println(uint); // -1
System.out.println(Integer.toUnsignedString(uint)); // 4294967295
}
回答by Eugene
As supplementary, if 64 bits long doesn't meet your requirement, try java.math.BigInteger.
作为补充,如果 64 位长不符合您的要求,请尝试java.math.BigInteger。
It's suitable for the situation that number is beyond the range of 64 bit long.
适用于数字超出64位长度范围的情况。
public static void main(String args[]){
String max_long = "9223372036854775807";
String min_long = "-9223372036854775808";
BigInteger b1 = new BigInteger(max_long);
BigInteger b2 = new BigInteger(min_long);
BigInteger sum = b1.add(b1);
BigInteger difference = b2.subtract(b1);
BigInteger product = b1.multiply(b2);
BigInteger quotient = b1.divide(b1);
System.out.println("The sum is: " + sum);
System.out.println("The difference is: " + difference);
System.out.println("The product is: " + product);
System.out.println("The quotient is: " + quotient);
}
The output is:
输出是:
The sum is: 18446744073709551614
总和为:18446744073709551614
The difference is: -18446744073709551615
区别是:-18446744073709551615
The product is: -85070591730234615856620279821087277056
产品为:-85070591730234615856620279821087277056
The quotient is: 1
商为:1