**Get started with Spring 5 and Spring Boot 2, through the ***Learn Spring* course:

*Learn Spring*course:

Last modified: May 17, 2022

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.

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

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

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:

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

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();
}
```

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

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

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

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

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

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

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:

```
@Test
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:

```
@Test
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:

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

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.

2 Comments

Oldest

Follow the Java Category

Follow the Java category to get regular info about the new articles and tutorials we publish here.