We need to perform a type casting to convert long to int in Java since we want to convert a higher data type to a lower one. Java type casting is the process of changing one data type into another.
See the following example:
class Test {
public static void main(String[] args) {
long l = 1083912;
int i = (int) l; // type casting
System.out.println(i);
}
}
Output: 1083912
We can also use the intValue() method of a Long class if we deal with the Long object instead of a primitive type.
Example
class Test {
public static void main(String[] args) {
Long l = new Long(1083912);
int i = l.intValue();
System.out.println(i);
}
}
Output: 1083912
That was all about how to convert long to int in Java. Proceed to the next lesson.
Happy coding!