Convert long to int Type in Java – 在Java中把长字符串转换为int类型

最后修改: 2022年 3月 14日

1. Overview


In this tutorial, we’ll see how we can convert a long value to an int type in Java. Before we start coding, we need to point out some details about this data type.


First of all, in Java, long values are represented by signed 64-bit numbers. On the other hand, int values are represented by signed 32-bit numbers. Therefore, converting a higher data type into a lower one is called narrowing type casting. As a result of these conversions, some bits would be lost when long values are larger than Integer.MAX_VALUE and Integer.MIN_VALUE.


Additionally, we’ll show for each conversion variant, how it works for a long value equals to Integer.MAX_VALUE plus one.


2. Data Conversion


2.1. Casting Values


First, casting values in Java is the most common way of type conversion – it’s straightforward:


public int longToIntCast(long number) {
    return (int) number;

2.2. Java 8

2.2.Java 8

Since Java 8, we can use two more ways to do type conversion: using the Math package or using a lambda function. For the Math package, we can use the toIntExact method:

从Java 8开始,我们又可以使用两种方式来进行类型转换:使用Math包或使用lambda函数。对于Math包,我们可以使用toIntExactmethod。

public int longToIntJavaWithMath(long number) {
return Math.toIntExact(number);

2.3. Wrapper Class


On the other hand, we can use the wrapper class Long to get the int value:


public int longToIntBoxingValues(long number) {
    return Long.valueOf(number).intValue();

2.4. Using BigDecimal


Moreover, we can accomplish this conversion using the BigDecimal class:


public static int longToIntWithBigDecimal(long number) {
    return new BigDecimal(number).intValueExact();

2.5. Using Guava


Next, we’ll show type conversion using Google Guava‘s Ints class:

接下来,我们将使用Google GuavaInts类展示类型转换。

public int longToIntGuava(long number) {
    return Ints.checkedCast(number);

In addition, Google Guava‘s Ints class provides a saturatedCast method:

此外,Google GuavaInts类提供了一个saturatedCast方法。

public int longToIntGuavaSaturated(long number) {
    return Ints.saturatedCast(number);

2.6. Integer Upper and Under Bounds


Finally, we need to consider that an integer value has an upper and under bound. These limits are defined by Integer.MAX_VALUE and Integer.MIN_VALUE. For values out of those limits, results are different from one method to another.


In the next code snippet, we’ll test the case when an int value is unable to hold the long value:


public void longToIntSafeCast() {
    long max = Integer.MAX_VALUE + 10L;
    int expected = -2147483639;
    assertEquals(expected, longToIntCast(max));
    assertEquals(expected, longToIntJavaWithLambda(max));
    assertEquals(expected, longToIntBoxingValues(max));

Using direct cast, with lambda or using boxing values produces a negative value. In those cases, the long value is greater than Integer.MAX_VALUE, that’s why the result value is wrapped with a negative number. If the long value is less than Integer.MIN_VALUE the result value is a positive number.


On the other hand, three of the methods described in this article could throw different types of exceptions:


public void longToIntIntegerException() {
    long max = Integer.MAX_VALUE + 10L;
    assertThrows(ArithmeticException.class, () -> ConvertLongToInt.longToIntWithBigDecimal(max));
    assertThrows(ArithmeticException.class, () -> ConvertLongToInt.longToIntJavaWithMath(max));
    assertThrows(IllegalArgumentException.class, () -> ConvertLongToInt.longToIntGuava(max));

For the first and the second one, an ArithmeticException is thrown. For the latter, an IllegalArgumentException is thrown. In that case, Ints.checkedCast checks if the integer is out of range.


Finally, from Guava, the saturatedCast method, first check on integer limits and return the limit value is the passed number is greater or lower than the integer upper and lower bounds:


public void longToIntGuavaSaturated() {
    long max = Integer.MAX_VALUE + 10L;
    int expected = 2147483647;
    assertEquals(expected, ConvertLongToInt.longToIntGuavaSaturated(max));

3. Conclusion


In this article, we went through some examples of how to convert long to int type in Java. Using native Java casting and some libraries.


As usual, all snippets used in this article are available over on GitHub.