Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Most efficient way to convert java.lang.Long to primitive int

I have a weird scenario where I need to convert several million java.lang.Longs into primitive int types. I need to do this several times a day, every single day. Normally, I wouldn't worry about this kind of simple casting, but since it's happening so much, so often, I have to ask: what's the most efficient way to do this, and why?

My first attempt:

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

Although this seems like going 3 sides around the barn. Thanks in advance.

like image 813
IAmYourFaja Avatar asked Jan 05 '13 15:01

IAmYourFaja


2 Answers

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

(warning, you may lose precision etc etc -- but you probably know that already)

like image 166
fge Avatar answered Oct 11 '22 04:10

fge


Try this

int x = myLong.intValue( );
like image 40
Alexander Pogrebnyak Avatar answered Oct 11 '22 04:10

Alexander Pogrebnyak