How can I convert a long to int in Java?

后端 未结 16 2164
余生分开走
余生分开走 2020-11-29 16:49

How can I convert a long to int in Java?

16条回答
  •  迷失自我
    2020-11-29 17:27

    Updated, in Java 8:

    Math.toIntExact(value);
    

    Original Answer:

    Simple type casting should do it:

    long l = 100000;
    int i = (int) l;
    

    Note, however, that large numbers (usually larger than 2147483647 and smaller than -2147483648) will lose some of the bits and would be represented incorrectly.

    For instance, 2147483648 would be represented as -2147483648.

提交回复
热议问题