long to int java

By Chaitanya Singh | Filed Under: Java Conversion

In this tutorial, we will see how to convert int to long with examples.

Since int is smaller data type than long, it can be converted to long with a simple assignment. This is known as implicit type casting or type promotion, compiler automatically converts smaller data type to larger data type. We can also convert int to long using valueOf() method of Long wrapper class.

Java int to long using implicit type casting

In the following example, we are simply assigning integer data type to a long data type. Since integer is a smaller data type compared to long, the compiler automatically converts int to long, this is known as type promotion.

Output:

Java int to long Example using Long.valueOf() method

In the following example, we are converting int to long using valueOf() method of Long Wrapper class. The valueOf() method accepts integer as an argument and returns a long value after the conversion.

Output:

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

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