Converting a long to an int in Java is a narrowing conversion, which means you’re converting from a larger data type (long, which is 64 bits) to a smaller data type (int, which is 32 bits). This type of conversion can potentially result in data loss if the long value is too large to fit into the int range.
1. Using Explicit Casting
Since this is a narrowing conversion, it requires explicit casting. When you cast a long to an int, you’re telling Java to convert the larger long value into a smaller int, even though there might be data loss.
Example:
public class LongToIntExample {
public static void main(String[] args) {
long longValue = 987654321L; // A long value (64 bits)
// Explicitly cast long to int
int intValue = (int) longValue; // Explicit casting
System.out.println(intValue); // Output: 987654321
}
}
Explanation:
1. In the example above, we declare a long variable (longValue) and explicitly cast it to an int using (int) longValue.
2. The value 987654321 fits within the int range, so the casting succeeds without loss of data.
3. The result is printed as 987654321.
2. Data Loss and Overflow Example
If the long value is outside the range of int (i.e., it is larger than Integer.MAX_VALUE or smaller than Integer.MIN_VALUE), casting it to int will cause data loss or overflow. In this case, the value will be truncated to fit within the int range, leading to an unexpected result.
Example:
public class LongToIntExample {
public static void main(String[] args) {
long longValue = 9876543210L; // A large long value (64 bits)
// Explicitly cast long to int (potential data loss)
int intValue = (int) longValue; // Casting to int
System.out.println(intValue); // Output will not be 9876543210
}
}
Explanation:
1. In the example above, longValue is set to 9876543210L, which is much larger than the maximum value that an int can hold (Integer.MAX_VALUE = 2^31 – 1, or 2,147,483,647).
2. When you cast this long value to an int, the value overflows and is truncated, resulting in an incorrect value. The actual result of the conversion will be a number that fits into the int range, but it is not the same as the original long value.
Output: -1610512450
3. How to Handle Overflow/Truncation
If you’re concerned about potential overflow when converting a long to an int, you can check if the long value is within the int range before performing the cast. This can help you avoid losing data.
Example:
public class LongToInt {
public static void main(String[] args) {
long longValue = 9876543210L;
// Check if the long value is within the int range before casting
if (longValue >= Integer.MIN_VALUE && longValue <= Integer.MAX_VALUE) {
int intValue = (int) longValue;
System.out.println("Converted value: " + intValue);
} else {
System.out.println("Overflow: The long value is outside the int range.");
}
}
}
Explanation:
1. Here, we check whether the long value is within the valid range for int before performing the cast. If the value is outside the range, we can handle the overflow case accordingly.
2. This ensures that you don’t lose data or get unexpected results.