How does Java handle integer underflows and overflows and how would you check for it?

前端 未结 12 1360
走了就别回头了
走了就别回头了 2020-11-21 06:40

How does Java handle integer underflows and overflows?

Leading on from that, how would you check/test that this is occurring?

12条回答
  •  我在风中等你
    2020-11-21 07:09

    There is one case, that is not mentioned above:

    int res = 1;
    while (res != 0) {
        res *= 2;
    
    }
    System.out.println(res);
    

    will produce:

    0
    

    This case was discussed here: Integer overflow produces Zero.

提交回复
热议问题