Scarlett Ideas Back to catalog
How to convert float to long or int data type in Java? Example
Aug 28, 2026 Scarlett

Java: Convert Long to Int

When working with numbers in Java, you might encounter the need to convert a long to an int. While Java facilitates this operation, it's essential to understand...

When working with numbers in Java, you might encounter the need to convert a long to an int. While Java facilitates this operation, it's essential to understand the implications and potential pitfalls. This article delves into the process, its potential issues, and best practices.

How to convert float to long or int data type in Java? Example

A long is a 64-bit two's complement integer that can range from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807. An int, on the other hand, is a 32-bit two's complement integer ranging from -2,147,483,648 to 2,147,483,647. Therefore, when converting a long to an int, precision might be lost if the long value exceeds the range of an int.

How to convert float to long or int data type in Java? Example

Explicit and Implicit Conversion

The most straightforward way to convert a long to an int in Java is by simply assigning the long value to an int variable. This is an example of implicit conversion, where Java automatically performs the conversion for you.

How to convert int value to a Long object in Java? Examples

Implicit conversion is convenient, but it might lead to unexpected results. If the long value is too large to fit into an int, Java will silently truncate the value, potentially leading to data loss or inconsistent results. For instance:

long longNum = 10000000000L;
int intNum = longNum; // intNum will be 256 instead of 10000000000

Using Casting

java

To avoid silent truncation, it's recommended to use explicit casting. Casting involves enclosing the value you want to convert in parentheses with the target data type, such as (int) for converting to an int. Here is an example:

```java long longNum = 10000000000L; int intNum = (int) longNum; // intNum will be -147483648 (actual value that can be represented by int) ```

Check for Range Before Casting

an orange and pink background with text on it

Sometimes, you might need to convert a long to an int, but the long value could exceed the range of an int. To avoid losing data, you should first check if the long value falls within the range of an int before performing the conversion:

```java long longNum = 10000000000L; if (longNum >= Integer.MIN_VALUE && longNum <= Integer.MAX_VALUE) { int intNum = (int) longNum; } ```

Loss of Precision and Truncation

an image of a computer screen with text

As mentioned earlier, converting a long to an int can lead to a loss of precision or data truncation if the long value is too large to fit into an int. It's crucial to be aware of this and handle these cases appropriately.

Performing a long to int conversion might also cause rounding errors. When a long is converted to an int, Java rounds towards zero. This means that if the fractional part is positive, the result is rounded down, and if the fractional part is negative, the result is rounded up:

Java Type Conversion with various examples
☕ Create a Java Temperature Converter in 5 Minutes
How To Convert Excel Data Into List Of Java Objects : Poiji API
Java Lang String Cannot Be Converted to JSONObject: The Hilarious Guide to Fixing JSON Drama
Java 8 - How to convert String to LocalDate and LocalDate to String in specific format
a computer screen with some type of programming program on it's side and another one in the background
an image of a computer screen with many lines on the screen and text below it
Core Java Coding / Programming Questions and Answers : Part 2
Payload Transformation: JSON to Java

```java long longNum = 15L; int intNum = (int) longNum; // intNum will be 15 ```

But for a negative number:

```java long longNum = -16L; int intNum = (int) longNum; // intNum will be -17 ```

In conclusion, while Java makes it easy to convert a long to an int, it's essential to understand the potential pitfalls and handle them appropriately. Always consider the range of values and the possibility of data loss or rounding errors. To avoid silent truncation, use explicit casting, and in certain cases, check the range before performing the conversion.

More shots

How to convert float to long or int data type in Java? Example
How to convert float to long or int data type in Java? Example
How to convert float to long or int data type in Java? Example
How to convert float to long or int data type in Java? Example
How to convert int value to a Long object in Java? Examples
How to convert int value to a Long object in Java? Examples
java
java
an orange and pink background with text on it
an orange and pink background with text on it
an image of a computer screen with text
an image of a computer screen with text
Java Type Conversion with various examples
Java Type Conversion with various examples
☕ Create a Java Temperature Converter in 5 Minutes
☕ Create a Java Temperature Converter in 5 Minutes

Related topics