How can a Java variable be different from itself?

前端 未结 10 1858
日久生厌
日久生厌 2020-11-29 16:14

I am wondering if this question can be solved in Java (I\'m new to the language). This is the code:

class Condition {
    // you can change in the main
    p         


        
10条回答
  •  被撕碎了的回忆
    2020-11-29 16:47

    The replaced line could read.

    double x = Double.NaN;
    

    This would cause the gotcha to be printed.

    Java Language Specification (JLS) says:

    Floating-point operators produce no exceptions (§11). An operation that overflows produces a signed infinity, an operation that underflows produces a denormalized value or a signed zero, and an operation that has no mathematically definite result produces NaN. All numeric operations with NaN as an operand produce NaN as a result. As has already been described, NaN is unordered, so a numeric comparison operation involving one or two NaNs returns false and any != comparison involving NaN returns true, including x!=x when x is NaN.

提交回复
热议问题