当您将整数增加到超出其最大值时会发生什么? [英] What happens when you increment an integer beyond its max value?

查看:1197
本文介绍了当您将整数增加到超出其最大值时会发生什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Java中,当你将int(或byte / short / long)增加到超出它的最大值时会发生什么?它是否回绕到最大负值?

In Java what happens when you increment an int (or byte/short/long) beyond it's max value ? Does it wrap around to the max negative value ?

AtomicInteger.getAndIncrement()的行为方式是否相同?

Does AtomicInteger.getAndIncrement() also behave in the same manner ?

推荐答案

来自整数运算的Java语言规范部分


内置整数运算符不
表示任何
方式的溢出或下溢。

The built-in integer operators do not indicate overflow or underflow in any way.

结果由语言指定且独立于JVM版本: Integer.MAX_VALUE的+ 1 == Integer.MIN_VALUE的 Integer.MIN_VALUE的 - 1 == Integer.MAX_VALUE的 。其他整数类型也是如此。

The results are specified by the language and independent of the JVM version: Integer.MAX_VALUE + 1 == Integer.MIN_VALUE and Integer.MIN_VALUE - 1 == Integer.MAX_VALUE. The same goes for the other integer types.

原子整数对象( AtomicInteger AtomicLong 等)在内部使用普通的整数运算符,所以 getAndDecrement()等也是这样的。

The atomic integer objects (AtomicInteger, AtomicLong, etc.) use the normal integer operators internally, so getAndDecrement(), etc. behave this way as well.

这篇关于当您将整数增加到超出其最大值时会发生什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆