为什么使用整型变量会引发异常? [英] Why does the use of integer variables throw an exception?

查看:5148
本文介绍了为什么使用整型变量会引发异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了以下两个代码。为什么它不会抛出一个浮点异常,因为在其他情况下它会抛出一个运行时异常。

 类FloatingPoint 
{
public static void main(String [] args)
{
float a = 1000f;
float b = a / 0;
System.out.println(b =+ b);




$ p $输出:b =无穷大。

如果我尝试使用int值,那么它会抛出一个运行时异常。
为什么会这样?

解决方案

简短的回答

b
$ b

积分类型( JLS 4.2.1 )与浮点类型明显不同( JLS 4.2.3 )。在行为和操作上可能有相似之处,但也有特征性的区别,使得两者混淆可能导致许多缺陷。

只是这些差异之一。因此,简单的答案是,Java的行为是这样的,因为语言是这样说的。






积分和浮点值



整数类型的值是以下范围的整数:


  • 字节:从 -128 127 code> [ - 2 7 ,2 7 -1]

  • code>> : -32768 32767 code> [ - 2 15 ,2 15 -1]

  • code> int :从 -2147483648 2147483647 ,包括 [ - 2 31 ,2 31 -1]

  • code> long :从 -9223372036854775808 9223372036854775807 code> [ - 2 63 ,2 63 -1]

  • char ,从'\\\''\\\￿'(含),即从 0 65535 ,即 [0,2 16 -1]



  • 浮点类型是 float double ,它们在概念上与单精度32位和双精度64位格式 IEEE 754 的值和操作。



    它们的值按如下顺序排列,从最小到最大: b
    $ b


    • 负无穷, li>
    • 负有限非零值,
    • 正负零(即 0.0 == -0.0 ),
    • 有限非零值,
    • 正无穷。


      此外还有一些特殊的 Not-a-Number NaN )值,它们是无序的。这意味着如果两个或两个操作数是 NaN




      • 数值比较运算符< < = > > = 返回 false

      • 数字相等运算符 返回 false

      • 数字不等式运算符!= / code>返回 true



      $ c> x!= x true 当且仅当 x NaN



      例如 double ,无穷和 NaN 可以被称为:



      这种情况类似于 float Float





      当抛出异常时



      数值运算只能抛出例外在这些情况下:


      1. NullPointerException ,如果需要对 null 引用进行拆箱转换

      2. ArithmeticException 如果需要进行装箱转换,则右侧为整数分隔/余数运算为零

      3. OutOfMemoryError 内存不足

      它们按重要性排序,关于陷阱的常见来源。一般来说:


      • 与所有其他引用类型一样,要特别小心box类型,它们可以是 null

      • 特别小心整数除法/余数运算的右边
      • 算术上溢/下溢不会导致抛出异常

      • 精确度损失 DOES NOT 导致抛出异常

      • 数学上不确定的浮点运算 DOES NOT 导致抛出异常






      • 除以0



        对于整数操作:


      • 除法和余数运算抛出 ArithmeticException 如果右边为零



      对于浮点操作操作:


      • 如果左操作数是 NaN 0 ,结果是 NaN

      • 如果操作是 division ,它会溢出并且结果是有符号的无穷大

      • 如果操作是 em>,结果是 NaN



      所有浮点的一般规则操作如下:


      • 溢出的操作会产生有符号的无穷大。
      • 下溢产生非规格化值或有符号零。
      • 没有数学上确定的结果的操作产生 NaN

      • 所有以 NaN 作为操作数的数字操作都会产生 NaN



      • $ hr
        $ b $ h $附录

        这个已经很长的答案没有涵盖的问题,但鼓励读者浏览相关的问题和参考资料。

        相关的问题




        • 这些三个特殊的浮点值是什么意思?

        • 在Java中,NaN是什么意思。 a>

        • Java什么时候能产生NaN(带有特定的代码问题)
        • -24-60-0-in-java />为什么(Java)中的(360/24)/ 60 = 0 ...(区分整数与浮点操作!)
          (小心盒装基元的危险!)
        • 1/0是合法的Java表达式吗?(绝对是!!!)

          I have come across with the following two codes. Why does it not throw an exception for floating point where as in other case it will throw a runtime exception.

          class FloatingPoint
              {
                public static void main(String [] args)
                 {
                   float a=1000f;
                   float b=a/0;
                  System.out.println("b=" +b);
                 }
              }
          

          OUTPUT:b=Infinity.

          If I try with int values then it will throw a runtime exception. Why is it like this?

          解决方案

          The short answer

          Integral types (JLS 4.2.1) are categorically different from floating point types (JLS 4.2.3). There may be similarities in behavior and operations, but there are also characteristically distinguishing differences such that confusing the two can lead to many pitfalls.

          The difference in behavior upon division by zero is just one of these differences. Thus, the short answer is that Java behaves this way because the language says so.


          On integral and floating point values

          The values of the integral types are integers in the following ranges:

          • byte: from -128 to 127, inclusive, i.e. [-27, 27-1]
          • short: from -32768 to 32767, inclusive, i.e. [-215, 215-1]
          • int: from -2147483648 to 2147483647, inclusive, i.e. [-231, 231-1]
          • long: from -9223372036854775808 to 9223372036854775807, inclusive, i.e. [-263, 263-1]
          • char, from '\u0000' to '\uffff' inclusive, that is, from 0 to 65535, i.e. [0, 216-1]

          The floating-point types are float and double, which are conceptually associated with the single-precision 32-bit and double-precision 64-bit format IEEE 754 values and operations.

          Their values are ordered as follows, from smallest to greatest:

          • negative infinity,
          • negative finite nonzero values,
          • positive and negative zero (i.e. 0.0 == -0.0),
          • positive finite nonzero values, and
          • positive infinity.

          Additionally, there are special Not-a-Number (NaN) values, which are unordered. This means that if either (or both!) operand is NaN:

          • numerical comparison operators <, <=, >, and >= return false
          • numerical equality operator == returns false
          • numerical inequality operator != returns true

          In particular, x != x is true if and only if x is NaN.

          For e.g. double, the infinities and NaN can be referred to as:

          The situation is analogous with float and Float.


          On when exceptions may be thrown

          Numerical operations may only throw an Exception in these cases:

          1. NullPointerException, if unboxing conversion of a null reference is required
          2. ArithmeticException, if the right hand side is zero for integer divide/remainder operations
          3. OutOfMemoryError, if boxing conversion is required and there is not sufficient memory

          They are ordered by importance, with regards to being common source for pitfalls. Generally speaking:

          • Be especially careful with box types, as just like all other reference types, they may be null
          • Be especially careful with the right hand side of an integer division/remainder operations
          • Arithmetic overflow/underflow DOES NOT cause an exception to be thrown
          • Loss of precision DOES NOT cause an exception to be thrown
          • A mathematically indefinite floating point operation DOES NOT cause an exception to be thrown

          On division by zero

          For integer operation:

          • Division and remainder operations throws ArithmeticException if the right hand side is zero

          For floating point operation:

          • If the left operand is NaN or 0, the result is NaN.
          • If the operation is division, it overflows and the result is a signed infinity
          • If the operation is remainder, the result is NaN

          The general rule for all floating point operation is as follows:

          • An operation that overflows produces a signed infinity.
          • An operation that underflows produces a denormalized value or a signed zero.
          • An operation that has no mathematically definite result produces NaN.
          • All numeric operations with NaN as an operand produce NaN as a result.

          Appendix

          There are still many issues not covered by this already long answer, but readers are encouraged to browse related questions and referenced materials.

          Related questions

          这篇关于为什么使用整型变量会引发异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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