CodeGym/Java Blog/Java Numbers/How to Convert long to int in Java
Author
John Selawsky
Senior Java Developer and Tutor at LearningTree

How to Convert long to int in Java

Published in the Java Numbers group
members
In this article, we are going to explain how to convert long to int in Java and write some code examples. Since long is a larger data type than int, here we should use type casting for such conversion. As you probably already know both long and int in Java are primitive data types. That means they are not class objects. All Objects and arrays are stored in Heap Space while primitive types such as int and long are stored in Stack memory. Long and int are somewhat similar. Integer means whole number, that can be positive, negative, or zero. Both long and int are integer types, so they are compatible in a way. The problem is, that the long type ranges from -263 to 263 – 1, or -9223372036854775808 to 9223372036854775807. That is, the Java program allocates 64 bits for a long type. At the same time, int type occupies 32 bits, in which you can put numbers from -231 до 231 - 1 or, which is the same, from -2147483648 to 2147483647. This means that any number of int type can easily fit into a long type. In Java, when working with primitives, the conversion from a narrower type to a wider type is automatic. In another way, it is called widening.
int myInt= 18;
long myLong= 16000;
       myLong = myInt;
Here we are assigning an int to a long variable. Everything went smoothly, in a larger range, a smaller one fits quietly and we do not lose anything except memory space. By the way, when we declare a variable of type long and define its value, it is better to assign the letter l to it, this will come in handy when we work with numbers outside the int range.
long myVeryLong = 10000000000l;

Java long to int conversion

Now let's get back to the main task of this article — Java long to int conversion. The problem is that something bigger doesn't always fit into something smaller. So here we can't just put “smaller” into “bigger” automatically in Java. If we try to act as in the previous example, but vice versa:
//example without typecasting…nice trying!
public static void main(String[] args) {

int myInt= 18;
long myLong= 16000;
       myInt = myLong;
}
Java compiler just won't let us do this and will throw an error. So for this operation, we need to use what is called typecasting. This process is called narrowing type conversion. To narrow a variable, you need to explicitly specify the type to which you want to cast your value. Here is an example where we are going to try to put one “small” long to int (that should fit in) and two “big” long numbers that are out of int range.
public class longToInt {

   public static void main(String[] args) {

       int myInt = 18;
       long myLong = 16000;
       long myVeryLong = 2147483648l;//l in the end means ‘long’
       long myVeryLong = 10000000000l;

       myInt = (int) myLong;
       System.out.println(myInt);
       myInt = (int) oneMoreLong;
       System.out.println(myInt);
       myInt = (int) myVeryLong;
       System.out.println(myInt);
   }
}
We have notified the compiler that we want to put the long value into an int variable and we are responsible for the consequences of this decision. The compiler, seeing an explicit indication of a narrower type, performs the conversion. As a result, we get the output:
16000 -2147483648 1410065408
Well, 16000 is definitely what we expected to see, but why is minus in -2147483648? And what does this 1410065408 mean? The fact is that such numbers were stored in a long variable, which, as we remember, takes 64 bits. We are trying to put these numbers into an int variable that can only store 32 bits. As a rule, these 32 cells will contain zeros and ones from the first 32 bits of the long number, and the rest will simply be discarded. Therefore, if the original number does not fit into 32 bits, then those bits that do not fit are simply discarded. That’s why we only have the correct number 16000, since it takes up less than 32 bits.

Java 8 long to int conversion

In Java 8, the Math class has a new method that will convert long to int. Here it is:
Math.toIntExact(value);
The best thing about this method is that it controls the length of the converted number, and if the value is too large to fit into an int, it will throw an exception. Let's see how this works in our example:
public class intToLong {

   public static void main(String[] args) {

       int myInt = 18;
       long myLong = 16000;
       long oneMoreLong = 2147483648l;
       long myVeryLong = 10000000000l;

       System.out.println(Math.toIntExact(myLong));
       int y = Math.toIntExact(oneMoreLong);
       System.out.println(oneMoreLong);
       System.out.println(Math.toIntExact(myVeryLong));
   }
}
The output is:
16000 Exception in thread "main" java.lang.ArithmeticException: integer overflow at java.base/java.lang.Math.toIntExact(Math.java:1080) at intToLong.main(intToLong.java:13)
Thus, the program printed the only correctly converted number 16000, and then when trying to push the out-of-range number 2147483648l into int, the method threw an exception. So we don’t have the problem of discarded bits, as in the case of classic Java long to int conversion.
Comments
  • Popular
  • New
  • Old
You must be signed in to leave a comment
This page doesn't have any comments yet