The most efficient way to convert java.lang.Long to a primitive int

I have a strange scenario where I need to convert several million java.lang.Long to primitive int types. I need to do this several times a day, every day. Normally, I would not worry about this simple casting, but since it happens so often, so often, I have to ask: what is the most effective way to do this and why?

My first attempt:

 Long myLong = getLong(); int x = Integer.valueOf(myLong.toString()) 

Although it looks like 3 sides around the barn. Thanks in advance.

+7
source share
3 answers

The Long class has a .intValue() method, I think this is what you are looking for ...

(warning, you may lose accuracy, etc.), but you probably already know that)

+23
source

try it

 int x = myLong.intValue( ); 
+9
source

try it

 Integer i = (int) (long) b; 
-2
source

All Articles