Converting Integer to Long
To convert an Integer
object to a Long
object in Java, you can use the longValue()
method of the Integer
class, which returns the value of the Integer
object as a long
.
For example:
Integer i = new Integer(123);
Long l = i.longValue();
Alternatively, you can use the longValue()
method of the Number
interface, which is implemented by the Integer
class. This allows you to use the same method to convert any object that implements the Number
interface to a long
, including Byte
, Short
, Integer
, and Long
objects:
Number n = new Integer(123);
Long l = n.longValue();
You can also use the longValueExact()
method of the Integer
class to convert an Integer
object to a Long
object. This method throws an ArithmeticException
if the value of the Integer
object is not within the range of the long
data type.
I hope this helps. Let me know if you have any questions.