编程学习笔记

System.out.println(1.0 / 0); 为什么

2018-12-29  本文已影响23人  烛火的咆哮

遇到了一个简单的细节, 做个记录,直接贴代码

    System.out.println(1.0 / 0);
    //下面报错
    System.out.println(1 / 0);
Infinity
Exception in thread "main" java.lang.ArithmeticException: / by zero
    at leetcode_normal.Test1.main(Test1.java:111)

再贴一下完整的代码

    System.out.println(1.0 / 0);
    System.out.println(1.0 / 0.0);
    System.out.println((long)(1.0 / 0.0));
    System.out.println(1 / 0);
Infinity
Infinity
Exception in thread "main" java.lang.ArithmeticException: / by zero
    at leetcode_normal.Test1.main(Test1.java:112)
9223372036854775807
    public static final double POSITIVE_INFINITY = 1.0 / 0.0;
    public static final double NEGATIVE_INFINITY = -1.0 / 0.0;
上一篇 下一篇

猜你喜欢

热点阅读