Java - convert long to int

13 points
Created by:


In java we can convert long to int by:
- using typecasting eg: (int) longValue
- Math.toIntExact
- Long.intValue

1. Using typecasting

long val = 1000L;
int intVal = (int) val;
System.out.println(intVal); // 1000

Type casting - when we assign value of one primitive data type to another type.
In this case from long to int.

2. Math.toIntExact

long val = 1000L;
int intVal = Math.toIntExact(val);
System.out.println(intVal); // 1000

Java 8 added method toIntExact to Math class.
If the passing long value overflows an int it will throw ArithmeticException.

3. Long.intValue

Long val = 1000L;
int intVal = val.intValue();
System.out.println(intVal); // 1000

Long class have intValue() method which allow us to perform typecasting.

Checkout latest findings & news:

Checkout latest questions:

Checkout latest wiki articles:

Hey 👋
Would you like to know what we do?
  • Dirask is a friendly IT community for learners, professionals and hobbyists to share their knowledge and help each other in extraordinary easy way.
  • We welcome everyone,
    no matter what the experience,
    no matter how basic the question is,
    this community will help you.
Rockstar community members
Thank you for great work 👍