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
.
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 1234567890
Alternatively, you can also use the Integer
constructor that takes a long
value as an argument:
Long l = 1234567890L;
Integer i = new Integer(l);
System.out.println(i); // Outputs 1234567890
It is important to note that if the value of the Long
is larger than the maximum value of an int
(2147483647
), the conversion will result in an integer overflow and the resulting Integer
will have a negative value. To avoid this, you can use the longValue()
method of the Integer
class, which returns the value of the Integer
as a long
.