Appearance
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. This method returns the value as a primitive long, which Java automatically wraps into a Long object via auto-boxing when assigned to a Long variable.
For example:
java
Integer i = 123;
Long l = i.longValue();Alternatively, you can use the longValue() method inherited from the Number interface, which is implemented by the Integer class. This allows you to convert any Number subclass (such as Byte, Short, Integer, or Long) to a long:
java
Number n = 123;
Long l = n.longValue();You can also assign the result directly to a primitive long to avoid unnecessary auto-boxing overhead:
java
Integer i = 123;
long primitiveLong = i.longValue();If you prefer explicit object conversion, you can use Long.valueOf(i.longValue()), though standard assignment handles it automatically.
I hope this helps. Let me know if you have any questions.