简体   繁体   中英

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.Long s 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.

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)

试试这个

int x = myLong.intValue( );

试试这个

Integer i = (int) (long) b;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM