Convert Long into Integer
To convert a Long object to an Integer object in Java, you can use the intValue() method of the Long class, which returns the value of the Long as an int.
To convert a Long object to an Integer object in Java, you can use the intValue() method of the Long class, which returns the value of the Long as an int.
Here is an example of how to convert a Long to an Integer:
Long l = 1234567890L;
Integer i = l.intValue();
System.out.println(i); // Outputs 1234567890Alternatively, you can use Integer.valueOf(long), which is the preferred modern approach since the Integer(long) constructor has been deprecated since Java 9:
Long l = 1234567890L;
Integer i = Integer.valueOf(l);
System.out.println(i); // Outputs 1234567890It is important to note that if the value of the Long exceeds the maximum value of an int (2147483647), the conversion will result in integer overflow, and the resulting Integer will wrap around to a negative value. To safely handle values outside the int range, check the bounds before converting, or keep the value as a long:
if (l >= Integer.MIN_VALUE && l <= Integer.MAX_VALUE) {
Integer i = l.intValue();
}