关于Integer越界的处理

刚才刷题, Leetcode中, Reverse Integer 和 String to Integer (atoi), 都要对结果进行越界处理. 什么是越界处理? 越界处理就是当整形数据超过最大/最小值的时候, 需要对数据进行判断, 避免程序出错. 可以用以下code进行对Integer的越界处理: if(Math.abs(res) > (Integer.MAX_VALUE – Math.abs(update_value)) / 10) return 0; 这里的res就是以上两题中, 每次需要做 res = res*10 + update_value的结果值, 我们在更新res前判断下次更新会不会越界(超过Integer.MAX_VALUE).