如何在 Java 中将 long 转换为 int?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4355303/
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
How can I convert a long to int in Java?
提问by Arthur
How can I convert a long to int in Java?
如何在 Java 中将 long 转换为 int?
回答by Theo
long x = 3;
int y = (int) x;
but that assumes that the long
can be represented as an int
, you do know the difference between the two?
但是假设long
可以表示为int
,您知道两者之间的区别吗?
回答by Frxstrem
Updated, in Java 8:
在 Java 8 中更新:
Math.toIntExact(value);
Original Answer:
原答案:
Simple type casting should do it:
简单的类型转换应该这样做:
long l = 100000;
int i = (int) l;
Note, however, that large numbers (usually larger than 2147483647
and smaller than -2147483648
) will lose some of the bits and would be represented incorrectly.
但是请注意,大数(通常大于2147483647
和小于-2147483648
)会丢失一些位并且会被错误地表示。
For instance, 2147483648
would be represented as -2147483648
.
例如,2147483648
将表示为-2147483648
。
回答by mndrix
For small values, casting is enough:
对于小值,强制转换就足够了:
long l = 42;
int i = (int) l;
However, a long
can hold more information than an int
, so it's not possible to perfectly convert from long
to int
, in the general case. If the long
holds a number less than or equal to Integer.MAX_VALUE
you can convert it by casting without losing any information.
但是, along
可以包含比 an 更多的信息int
,因此在一般情况下不可能完美地从 转换long
为int
。如果long
持有一个小于或等于的数字,Integer.MAX_VALUE
您可以通过强制转换来转换它而不会丢失任何信息。
For example, the following sample code:
例如,以下示例代码:
System.out.println( "largest long is " + Long.MAX_VALUE );
System.out.println( "largest int is " + Integer.MAX_VALUE );
long x = (long)Integer.MAX_VALUE;
x++;
System.out.println("long x=" + x);
int y = (int) x;
System.out.println("int y=" + y);
produces the following output on my machine:
在我的机器上产生以下输出:
largest long is 9223372036854775807
largest int is 2147483647
long x=2147483648
int y=-2147483648
Notice the negative sign on y
. Because x
held a value one larger than Integer.MAX_VALUE, int y
was unable to hold it. In this case it wrapped around to the negative numbers.
注意 上的负号y
。因为x
持有一个比Integer.MAX_VALUE大一的值,int y
无法持有它。在这种情况下,它环绕到负数。
If you wanted to handle this case yourself, you might do something like:
如果您想自己处理这种情况,您可以执行以下操作:
if ( x > (long)Integer.MAX_VALUE ) {
// x is too big to convert, throw an exception or something useful
}
else {
y = (int)x;
}
All of this assumes positive numbers. For negative numbers, use MIN_VALUE
instead of `MAX_VALUE'
所有这些都假设为正数。对于负数,使用MIN_VALUE
代替`MAX_VALUE'
回答by Alexis Dufrenoy
In Java, a long is a signed 64 bits number, which means you can store numbers between -9,223,372,036,854,775,808 and 9,223,372,036,854,775,807 (inclusive).
在 Java 中,long 是一个有符号的 64 位数字,这意味着您可以存储 -9,223,372,036,854,775,808 和 9,223,372,036,854,775,807(含)之间的数字。
A int, on the other hand, is signed 32 bits number, which means you can store number between -2,147,483,648 and 2,147,483,647 (inclusive).
另一方面,int 是有符号的 32 位数字,这意味着您可以存储 -2,147,483,648 和 2,147,483,647(含)之间的数字。
So if your long is outside of the values permitted for an int, you will not get a valuable conversion.
因此,如果您的 long 超出了 int 允许的值,您将不会获得有价值的转换。
Details about sizes of primitive Java types here:
有关原始 Java 类型大小的详细信息,请访问:
http://download.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html
http://download.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html
回答by eleonzx
You can use the Long
wrapper instead of long
primitive and call
您可以使用Long
包装器代替long
原语并调用
Long.intValue()
It rounds/truncate the long
value accordingly to fit in an int
.
它相应地舍入/截断long
值以适合int
.
回答by Andrej Herich
If using Guava library, there are methods Ints.checkedCast(long)
and Ints.saturatedCast(long)
for converting long
to int
.
如果使用番石榴库,有方法Ints.checkedCast(long)
,并Ints.saturatedCast(long)
转换long
到int
。
回答by tsaowe
In Spring, there is a rigorous way to convert a long to int
在 Spring 中,有一种严格的方式将 long 转换为 int
not only lnog can convert into int,any type of class extends Number can convert to other Number type in general,here I will show you how to convert a long to int,other type vice versa.
不仅 lnog 可以转换为 int,任何类型的类 extends Number 都可以转换为其他 Number 类型,这里我将向您展示如何将 long 转换为 int,其他类型反之亦然。
Long l = 1234567L;
int i = org.springframework.util.NumberUtils.convertNumberToTargetClass(l, Integer.class);
回答by Kristijan Dra?a
Long l = 100;
int i = Math.round(l);
回答by Laxman
Long x = 100L;
int y = x.intValue();
回答by Kuchi
Since Java 8 you can use: Math.toIntExact(long value)
从 Java 8 开始,您可以使用: Math.toIntExact(long value)
Returns the value of the long argument; throwing an exception if the value overflows an int.
返回长参数的值;如果值溢出 int 则抛出异常。
Source code of Math.toIntExact
in JDK 8:
Math.toIntExact
JDK 8 中的源代码:
public static int toIntExact(long value) {
if ((int)value != value) {
throw new ArithmeticException("integer overflow");
}
return (int)value;
}