Srinivasan Srinivasan - 3 months ago 20
Java Question

Convert Long into Integer

How to convert a

value into an
Integer
value in Java?

Answer
Integer i = theLong != null ? theLong.intValue() : null;

or if you don't need to worry about null:

// auto-unboxing does not go from Long to int directly, so
Integer i = (int) (long) theLong;

And in both situations, you might run into overflows (because a Long can store a wider range than an Integer).