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

前端 未结 12 1378
走了就别回头了
走了就别回头了 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:10

    I think this should be fine.

    static boolean addWillOverFlow(int a, int b) {
        return (Integer.signum(a) == Integer.signum(b)) && 
                (Integer.signum(a) != Integer.signum(a+b)); 
    }
    

提交回复
热议问题