<legend id='LV9h8'><style id='LV9h8'><dir id='LV9h8'><q id='LV9h8'></q></dir></style></legend>
    <tfoot id='LV9h8'></tfoot>

      <i id='LV9h8'><tr id='LV9h8'><dt id='LV9h8'><q id='LV9h8'><span id='LV9h8'><b id='LV9h8'><form id='LV9h8'><ins id='LV9h8'></ins><ul id='LV9h8'></ul><sub id='LV9h8'></sub></form><legend id='LV9h8'></legend><bdo id='LV9h8'><pre id='LV9h8'><center id='LV9h8'></center></pre></bdo></b><th id='LV9h8'></th></span></q></dt></tr></i><div id='LV9h8'><tfoot id='LV9h8'></tfoot><dl id='LV9h8'><fieldset id='LV9h8'></fieldset></dl></div>
        <bdo id='LV9h8'></bdo><ul id='LV9h8'></ul>

        <small id='LV9h8'></small><noframes id='LV9h8'>

      1. Java 如何处理整数下溢和上溢以及如何检查它?

        时间:2023-10-01

          <tbody id='NSYFO'></tbody>

              <bdo id='NSYFO'></bdo><ul id='NSYFO'></ul>
              1. <i id='NSYFO'><tr id='NSYFO'><dt id='NSYFO'><q id='NSYFO'><span id='NSYFO'><b id='NSYFO'><form id='NSYFO'><ins id='NSYFO'></ins><ul id='NSYFO'></ul><sub id='NSYFO'></sub></form><legend id='NSYFO'></legend><bdo id='NSYFO'><pre id='NSYFO'><center id='NSYFO'></center></pre></bdo></b><th id='NSYFO'></th></span></q></dt></tr></i><div id='NSYFO'><tfoot id='NSYFO'></tfoot><dl id='NSYFO'><fieldset id='NSYFO'></fieldset></dl></div>

                <legend id='NSYFO'><style id='NSYFO'><dir id='NSYFO'><q id='NSYFO'></q></dir></style></legend>

                  <small id='NSYFO'></small><noframes id='NSYFO'>

                  <tfoot id='NSYFO'></tfoot>

                  本文介绍了Java 如何处理整数下溢和上溢以及如何检查它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  Java 如何处理整数下溢和上溢?

                  How does Java handle integer underflows and overflows?

                  在此基础上,您将如何检查/测试这种情况是否正在发生?

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

                  推荐答案

                  如果溢出,返回到最小值 并从那里继续.如果下溢,则返回 最大值 并从那里继续.

                  If it overflows, it goes back to the minimum value and continues from there. If it underflows, it goes back to the maximum value and continues from there.

                  您可以按如下方式预先检查:

                  You can check that beforehand as follows:

                  public static boolean willAdditionOverflow(int left, int right) {
                      if (right < 0 && right != Integer.MIN_VALUE) {
                          return willSubtractionOverflow(left, -right);
                      } else {
                          return (~(left ^ right) & (left ^ (left + right))) < 0;
                      }
                  }
                  
                  public static boolean willSubtractionOverflow(int left, int right) {
                      if (right < 0) {
                          return willAdditionOverflow(left, -right);
                      } else {
                          return ((left ^ right) & (left ^ (left - right))) < 0;
                      }
                  }
                  

                  (您可以将 int 替换为 long 以对 long 执行相同的检查)

                  (you can substitute int by long to perform the same checks for long)

                  如果您认为这种情况可能经常发生,请考虑使用可以存储更大值的数据类型或对象,例如long 或者 java.math.BigInteger.最后一个不溢出,实际可用的JVM内存就是极限了.

                  If you think that this may occur more than often, then consider using a datatype or object which can store larger values, e.g. long or maybe java.math.BigInteger. The last one doesn't overflow, practically, the available JVM memory is the limit.

                  如果您碰巧已经在使用 Java8,那么您可以使用新的 Math#addExact()Math#subtractExact() 方法会抛出 ArithmeticException 溢出.

                  If you happen to be on Java8 already, then you can make use of the new Math#addExact() and Math#subtractExact() methods which will throw an ArithmeticException on overflow.

                  public static boolean willAdditionOverflow(int left, int right) {
                      try {
                          Math.addExact(left, right);
                          return false;
                      } catch (ArithmeticException e) {
                          return true;
                      }
                  }
                  
                  public static boolean willSubtractionOverflow(int left, int right) {
                      try {
                          Math.subtractExact(left, right);
                          return false;
                      } catch (ArithmeticException e) {
                          return true;
                      }
                  }
                  

                  源码可以找到这里 和 这里分别.

                  The source code can be found here and here respectively.

                  当然,您也可以直接使用它们,而不是将它们隐藏在 boolean 实用程序方法中.

                  Of course, you could also just use them right away instead of hiding them in a boolean utility method.

                  这篇关于Java 如何处理整数下溢和上溢以及如何检查它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:Mockito、JUnit、Hamcrest、版本控制 下一篇:为什么在比较 Java 中的整数包装器时 128==128 为假

                  相关文章

                  最新文章

                • <legend id='QUAUz'><style id='QUAUz'><dir id='QUAUz'><q id='QUAUz'></q></dir></style></legend>

                  <tfoot id='QUAUz'></tfoot>
                    <bdo id='QUAUz'></bdo><ul id='QUAUz'></ul>

                      <small id='QUAUz'></small><noframes id='QUAUz'>

                      <i id='QUAUz'><tr id='QUAUz'><dt id='QUAUz'><q id='QUAUz'><span id='QUAUz'><b id='QUAUz'><form id='QUAUz'><ins id='QUAUz'></ins><ul id='QUAUz'></ul><sub id='QUAUz'></sub></form><legend id='QUAUz'></legend><bdo id='QUAUz'><pre id='QUAUz'><center id='QUAUz'></center></pre></bdo></b><th id='QUAUz'></th></span></q></dt></tr></i><div id='QUAUz'><tfoot id='QUAUz'></tfoot><dl id='QUAUz'><fieldset id='QUAUz'></fieldset></dl></div>