How can I convert a long to int in Java?
long x = 3;
int y = (int) x;
but that assumes that the long
can be represented as an int
, you do know the difference between the two?
You can use the Long
wrapper instead of long
primitive and call
Long.intValue()
Java7 intValue() docs
It rounds/truncate the long
value accordingly to fit in an int
.
If using Guava library, there are methods Ints.checkedCast(long) and Ints.saturatedCast(long) for converting long
to int
.
If direct casting shows error you can do it like this:
Long id = 100;
int int_id = (int) (id % 100000);