Top community members
All Wiki Articles Create Wiki Article

Welcome to Dirask IT community! ❤ 💻
We are community of people that helps each other.

If you are beginner in IT field, you are more then welcome to ask questions, it will help you to learn faster. We are here to help you.

We are always beginner in something, we just need to remember it along the way.

there are no wrong questions - Ask Question

Java - convert long to int

0 contributions
13 points


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.

0 contributions

Checkout latest Findings & News:

Checkout latest questions:

Checkout latest wiki articles:

Hey 👋
Would you like to know what we do?
  • Dirask is IT community, where we share coding knowledge and help each other to solve coding problems.
  • We welcome everyone,
    no matter what the experience,
    no matter how basic the question is,
    this community will help you.
Read more