Examples
- How to Convert Char to String in Java? Character to String
- How to Convert Char to Int in Java? Character to Integer Conversion
- How to Convert Int to Char in Java? Integer to Character
- How to Convert Long to Int in Java? Long to Integer
- How to Convert Int to Long in Java? Integer to Long Program
- How to Convert Boolean to String in Java? (Parse Bool to String)
- How to Convert String to Boolean in Java? 3 Ways With Program
- How to Convert String to Int in Java? String to Integer Program
- How to Convert Int to String in Java? Integer to String Program
- How to Convert Int to Double in Java? (Integer to Double)
- How to Convert Double to Int in Java? 4 Ways
- How to Convert String to Double in Java? Programs
- How to Convert Double to String in Java? Programs
How to Convert Long to Int in Java? Long to Integer
Java empowers developers to tackle diverse challenges with its rich set of data types. When dealing with vast datasets or performing complex calculations, you may come across situations where data needs to be converted between different numeric types to ensure efficient processing and accurate results.
In this post, we will learn how to convert long to int in Java. The long data type, with its extended range, allows programmers to work with large numerical values, while the int data type offers optimal performance for most general-purpose computations.
However, converting long to int in Java requires careful consideration, as it may lead to potential data loss due to truncation.
Hence, we will discuss various strategies for converting long to int while safeguarding against precision errors and overflow.
Understanding the significance of data type conversions is crucial, as it influences the behavior of arithmetic operations, comparisons, and the overall correctness of your Java programs.
Convert Long to Int in Java Using Typecasting
We can use typecasting in Java for long to int conversion. Typecasting allows you to explicitly convert a value of one data type to another data type, provided that the conversion is valid and the value fits within the range of the target data type.
Code
public class LongToIntExample {
public static void main(String[] args) {
long myLong = 123456789L;
int myInt = (int) myLong; // Typecasting long to int
// Print the converted int
System.out.println("Converted int: " + myInt);
}
}
Output
Converted int: 123456789
Explanation
We have a long variable myLong with the value 123456789L. We then use typecasting (int) myLong to convert the long to an int.
It's important to note that typecasting from long to int may result in data loss if the long value is outside the valid range of int (-2,147,483,648 to 2,147,483,647 in Java). If the long value exceeds this range, it will be truncated, and the result may not be accurate.
Convert Long to Int in Java Using toIntExact()
You can convert a long to an int in Java using the toIntExact() method provided by the Math class. The toIntExact() method is specifically designed for converting a long value to an int while checking for possible overflow.
Code
public class LongToIntExample {
public static void main(String[] args) {
long myLong = 123456789L;
try {
int myInt = Math.toIntExact(myLong); // Convert long to int using toIntExact()
System.out.println("Converted int: " + myInt);
} catch (ArithmeticException e) {
System.out.println("Conversion resulted in overflow. Value is too large for int.");
}
}
}
Output
Converted int using toIntExact(): 123456789
Explanation
In this example, we have a long variable myLong with the value 123456789L. We then use Math.toIntExact(myLong) to convert the long to an int.
The toIntExact() method will throw an ArithmeticException if the long value is outside the valid range of int (-2,147,483,648 to 2,147,483,647 in Java). This makes it safer to perform the conversion as it explicitly checks for overflow, preventing any potential data loss.
As you can see, the long value 123456789 has been successfully converted to the int representation 123456789 using the toIntExact() method.
This method is a reliable way to convert a long to an int when you need to ensure that the value does not exceed the valid range of the int data type. If the long value is too large to fit into an int, the toIntExact() method will throw an exception, allowing you to handle such cases appropriately.
Long to Int Conversion Using intValue() Method
We can also convert a Long object to an int primitive type using the intValue() method of the Long class. This method is a part of the Number class, which is a superclass of the Long class, and it allows you to extract the int value from the Long object.
Code
public class LongToIntExample {
public static void main(String[] args) {
Long myLongObj = 123456789L; // Autoboxing: long to Long
int myInt = myLongObj.intValue(); // Convert Long to int using intValue()
// Print the original Long object and the converted int
System.out.println("Original Long object: " + myLongObj);
System.out.println("Converted int using intValue(): " + myInt);
}
}
Output
Converted int using intValue(): 123456789
Explanation
In this example, we have a Long object myLongObj with the value 123456789L. We then use myLongObj.intValue() to extract the int value from the Long object and store it in the myInt variable.
Learn Next: