Java - Convert long to int

1
2
long l = 1;
int i = (int) l;


To do it safely:

1
2
3
4
5
6
7
public static int convertLongToInt(long l) {
    if (l < Integer.MIN_VALUE || l > Integer.MAX_VALUE) {
        throw new IllegalArgumentException
            ("Invalid value for: " + l);
    }
    return (int) l;
}