How to convert long to int in Java?

There are different ways of converting a long to an int in Java.

There is a reason that our list does not contain direct typecasting of long into an int because it can lead to integer overflow and you may end up using the wrong value. Below is an example of the same.

public class Codekru {

	public static void main(String[] args) {

		long l = 12123456789034567L;
		int i = (int) l; // typecasting into int

		System.out.println("int value = " + i);
	}
}

Output –

int value = 1063007815

So, here we can see that in these kinds of scenarios, we may end up with incorrect values. We can use the Math.toIntExact() method of the Math class to resolve this.

Math.toIntExact() method

Method declaration – public static int toIntExact(long value).
What does it do? This method came with Java 8 and it will convert the long value passed into the argument to an int value, but with a twist that if integer overflows, then it will throw ArithmeticException.
What does it return? It will return the int if it is successfully converted, otherwise it will throw ArithmeticException.

Internal implementation of toIntExact() method

    public static int toIntExact(long value) {
        if ((int)value != value) {
            throw new ArithmeticException("integer overflow");
        }
        return (int)value;
    }

Now, let’s convert our long to int using this method.

public class Codekru {

	public static void main(String[] args) {

		long l = 12123456L;
		int i = Math.toIntExact(l);

		System.out.println("int value = " + i);
	}
}

Output –

int value = 12123456

Now, let’s try to take a number that will overflow the int value.

public class Codekru {

	public static void main(String[] args) {

		long l = 12123456789034567L;
		int i = Math.toIntExact(l);

		System.out.println("int value = " + i);
	}
}

Output –

Exception in thread "main" java.lang.ArithmeticException: integer overflow
	at java.base/java.lang.Math.toIntExact(Math.java:1080)

We can see here that it gave us an exception and does we can handle the further execution of our code based on this. That’s why we recommend using this method to convert long into an int in Java.

intValue() method

Method declaration – public int intValue()
What does it do? It is a function of the Long class in Java and it will also convert long into an int value using typecasting internally.
What does it return? It will return the int value.

public class Codekru {

	public static void main(String[] args) {

		Long l = 1234567890L;
		int i = l.intValue();

		System.out.println("int value = " + i);
	}
}

Output –

int value = 1234567890

But we don’t recommend this method because it internally uses typecasting and will not throw any exception if the long value is too big to fit into the int data type.

Internal implementation of intValue() function

    public int intValue() {
        return (int)value;
    }

Using Strings

Here, we have to first convert long to String and then String into an int. Below is the code example for the same.

public class Check {

	public static void main(String[] args) {

		long l = 12345678L;
		String longString = String.valueOf(l);

		int intValue = Integer.parseInt(longString);

		System.out.println("int value = " + intValue);

	}
}

Output –

int value = 12345678

This is rather a long way to convert string to int compared with the other two and off all the methods, we would recommend converting long to int using the Math.toIntExact() method.

Hope you have liked the article. If you have any doubts or concerns, please feel free to write us in comments or mail us at [email protected]

Liked the article? Share this on

Leave a Comment

Your email address will not be published.