java long to integer

By Chaitanya Singh | Filed Under: Java Conversion

In this guide, we will see how to convert long to int with examples. Since long is larger data type than int, we need to explicitly perform type casting for the conversion.

Java long to int example

In the following example we are converting long data type to int data type using explicit typecasting. Here we have a variable lnum of long data type and we are converting the value of it into an int value which we are storing in the variable inum of int data type.

Output:

Java long to int example using intValue() method of Long wrapper class

In the following example, we have a Long object lnum and we are converting it into an int primitive type using intValue() method of Long class.

Output:

How to convert a Long value into an Integer value in Java?

12 Answers 12

or if you don’t need to worry about null:

And in both situations, you might run into overflows (because a Long can store a wider range than an Integer).

Java 8 has a helper method that checks for overflow (you get an exception in that case):

Here are three ways to do it:

All three versions generate almost identical byte code:

For non-null values:

If you care to check for overflows and have Guava handy, there is Ints.checkedCast() :

You’ll need to type cast it.

Bear in mind that a long has a bigger range than an int so you might lose data.

If you are talking about the boxed types, then read the documentation.

If you are using Java 8 Do it as below

The best simple way of doing so is:

Assuming not null longVal

It works for example y you get an Object that can be an Integer or a Long. I know that is ugly, but it happens.

In Java 8 you can use toIntExact . If you want to handle null values, use:

Good thing about this method is that it throws an ArithmeticException if the argument ( long ) overflows an int .

Using toIntExact(long value) returns the value of the long argument, throwing an exception if the value overflows an int. it will work only API level 24 or above.

long visitors =1000;

int convVisitors =(int)visitors;

In java ,there is a rigorous way to convert a long to int

not only lnog can convert into int,any type of class extends Number can convert to other Number type in general,here I will show you how to convert a long to int,other type vice versa.

Not the answer you’re looking for? Browse other questions tagged java or ask your own question.

Linked

Hot Network Questions

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

site design / logo © 2019 Stack Exchange Inc; user contributions licensed under cc by-sa 4.0 with attribution required. rev 2019.11.15.35459

long to int Java

After knowing primitive data types and Java rules of Data Type Casting (Type Conversion), let us cast long to int.

The long takes 8 bytes of memory and int take 4 bytes of memory. Assigning 8 bytes of memory to 4 bytes of memory requires explicit casting.

byte –> short –> int –> long –> float –> double

The left-side value can be assigned to any right-side value and is done implicitly. The reverse like long to int requires explicit casting.

Examples of implicit casting

int x = 10; // 4 bytes
long y = x; // 4 bytes to 8 bytes

byte x = 10; // 1 byte
int y = x; // 1 byte to 4 bytes

Following program explains explicit casting, Java style where long is type converted to int.

Оцените статью
SoftLast
Добавить комментарий