gpt4 book ai didi

java - 将 Java 增量远远超过最大值时的行为

转载 作者:行者123 更新时间:2023-12-01 08:05:58 24 4
gpt4 key购买 nike

考虑:

long n = Long.MAX_VALUE;
++n;

对于我的 JVM,这会返回到 Long.MIN_VALUE。这是定义的行为吗?有些 JVM 会抛出异常吗?

最佳答案

来自 JLS:

If an integer addition overflows, then the result is the low-order bits of the mathematical sum as represented in some sufficiently large two's-complement format. If overflow occurs, then the sign of the result is not the same as the sign of the mathematical sum of the two operand values.

来源:http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.18.2

关于java - 将 Java 增量远远超过最大值时的行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21624156/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com