Table of Contents
Different methods to convert long to int in Java
In Java, the long and an int are a primitive data types. In real life applications, many times we will need to convert the value of one type to the other type. So, Java provides the mechanism for this conversion. As we know, the long type can store larger values than int type. So, when long value is assigned to int type variable, it leads to narrowing conversion.
Here, we will see several ways to convert a long to its equivalent int.
- Using Explicit Type Casting
- Using
intValue()
method of Long Class - Using
Math.toIntExact
method
Method-1: Using Explicit Type casting
This is the simplest approach to convert long to int in Java. Here, this is going to be narrowing conversion. So, if the value stored in the long variable is greater than Integer.MAX_VALUE
(2147483647) and less than Integer.MIN_VALUE
(-2147483648) then it will lead to compile time error. So, while using Type casting we need to ensure that the long value is between the range of the integer variable.
Example : In this example, we are showing explicit type conversions.
// Program to convert long to int
public class Main {
public static void main(String[] args) {
// Initializing the variables
long a = 1598755584;
long b = -5288745;
// Explicit type conversion
int a1 = (int) a;
int b1 = (int) b;
System.out.println("Converting from long to int " + a + " is " + a1);
System.out.println("Converting from long to int " + b + " is " + b1);
}
}
Output
Converting from long to int1598755584 is 1598755584
Converting from long to int -5288745 is -5288745
Method-2: Using intValue() method of Long Class
In this approach, we are using the method intValue of Long wrapper class to convert long to int value. It returns the value of this Long object as an int after a narrowing primitive conversion. Moreover, if the long value is not in range of integer than it will return a equivalent negative value.
Example : In this example, we are taking two long objects one with the value greater than that supported by integer and other value is within the range of integer.
// Program to convert long to int in Java
public class Main {
public static void main(String[] args) {
// Initializing the variables
Long l = 258741258L;
Long l1 = 2587412369L;
// Converting long to int and printing
System.out.println("Converting from long to int " + l + " is " + l.intValue());
System.out.println("Converting from long to int " + l1 + " is " + l1.intValue());
}
}
Output
Converting from long to int 258741258 is 258741258
Converting from long to int 2587412369 is -1707554927
Method-3: Using Math.toIntExact() method
We can use this approach from Java 8 onwards to convert long to int value. Here, we can use the built-in Math class method which is Math.toIntExac()
to convert a long to an int in Java. This is a static method of Math class of java.lang
package that returns the value of the long argument as an int. Moreover, it will throw an ArtihmeticException
if the value overflows an int.
Example : In this example, long value is converted to int and when the long value does not fit in int variable it throws an ArithmeticException
.
// Program to convert long to int in java
public class Main {
public static void main(String[] args) {
// Initializing the variables
Long l = 258741258L;
Long l1 = 2587412369L;
int i = Math.toIntExact(l);
System.out.println("Converting from long to int " + l + " is " + i);
i = Math.toIntExact(l1);
System.out.println("Converting from long to int " + l1 + " is " + i);
}
}
Output
Converting from long to int 258741258 is 258741258 Exception in thread "main" java.lang.ArithmeticException: integer overflow at java.base/java.lang.Math.toIntExact(Math.java:1071) at Main.main(Main.java:12)
Java Programming Examples on long to int conversion
Example 1 : Computing area of rectangle
In this example, we are computing the area of rectangle and converting it to int value.
// Program to demonstrate convert long to int in java
public class Main {
public static void main(String[] args) {
// Initializing the variables
Long w = 1000L;
Long l = 12584L;
// Computing area of rectangle and converting long to int
l = l * w;
System.out.println("Computing area of rectangle with length " + l + " and width " + w + " is " + l.intValue());
}
}
Output
Computing area of rectangle with length 12584000 and width 1000 is 12584000
Example 2 : Find the value of series of numbers
In this example, we are finding the summation of the numbers after converting the numbers to equivalent int value.
// Program to demonstrate convert long to int in java
public class Main {
public static void main(String[] args) {
// Initializing the variables
Long l = 10000000L;
Long sum = 0L;
// Computing area of rectangle and printing
System.out.println("Computing the result of l + 10l + 20l + ...+ 50l");
for (int i = 1; i <= 5; i++) {
sum = sum + (l * i * 10);
}
System.out.println("Originaly sum is " + sum);
System.out.println("After conversion to int Result is " + sum.intValue());
// Updating the value of l and recalculating
l = 10000000L;
System.out.println("\n\nComputing the result of l + 10l + 20l + ...+ 100l");
for (int i = 1; i <= 10; i++) {
sum = sum + (l * i * 10);
}
System.out.println("Originaly sum is " + sum);
System.out.println("After conversion to int Result is " + sum.intValue());
}
}
Output
Computing the result of l + 10l + 20l + ...+ 50l
Originaly sum is 1500000000
After conversion to int Result is 1500000000
Computing the result of l + 10l + 20l + ...+ 100l
Originaly sum is 7000000000
After conversion to int Result is -1589934592
Example 3 : Find the volume of Cube
In this example, we are finding the volume of cube stored as a long value and converting it to int value.
// Program to demonstrate convert long to int in java
public class Main {
public static void main(String[] args) {
// Initializing the variables
long w = 1000L;
// Computing volume of cube and printing
int v = (int)(w * w * w);
System.out.println("Computing volume of cube with edge = " + w + " Result is " + v);
}
}
Output
Computing volume of cube with edge = 1000 Result is 1000000000
Example 4 : Compute the Simple Interest
In this example, we are computing the simple interest using the principal stored as a long value. We are also computing the total amount obtained at the end of duration.
// Program to convert long to int in java
public class Main {
public static void main(String[] args) {
// Initializing the variables
long p = 10000000L;
int r = 10;
int n = 3;
// Computing volume of cube and printing
Long si = (p * r * n) / 100;
System.out.println("Principal is " + p + " Rate is " + r + " Duration in year is " + n);
System.out.println("Computing simple interest. Result will be :" + Math.toIntExact(si));
System.out.println("Total amount at the end of duration will be " + Math.toIntExact(si + p));
}
}
Output
Principal is 10000000 Rate is 10 Duration in year is 3
Computing simple interest. Result will be :3000000
Total amount at the end of duration will be 13000000
Summary
The knowledge of converting long to an int in Java is very useful while working on real time applications. This is referred as a narrowing conversion. In this tutorial, we covered three different approaches to convert long to int in Java. As per the requirement of an application, we can choose an appropriate approach for conversion.
We learned in detail about this approaches with an example. All in all, this tutorial, covers everything that you need to know in order to have a clear view on converting long to int value in Java.
References
Conversions and Promotions
Long class
Math class