Java autoboxing rules

前端 未结 4 1703
难免孤独
难免孤独 2020-12-09 06:04

I am a java novice and so confused by the following example. Is it okay to think that \"==\" sign will compare the values between Integers and \"autoboxed\" Integer

4条回答
  •  醉话见心
    2020-12-09 06:35

    Here is the Tutorial for Autoboxing and Unboxing.

    You can also go through JLS#5.1.7. Boxing Conversion and JLS#5.1.8. Unboxing Conversion

    0.0 / 0.0 is NaN you can not compare infinity at least in maths. I guess that is why this comparison does not work.

    From JLS #4.2.3. Floating-Point Types, Formats, and Values

    Positive zero and negative zero compare equal; thus the result of the expression 0.0==-0.0 is true and the result of 0.0>-0.0 is false

    NaN is unordered, so:

    • The numerical comparison operators <, <=, >, and >= return false if either or both operands are NaN (§15.20.1).

    • The equality operator == returns false if either operand is NaN.

    • In particular, (x=y) will be false if x or y is NaN.

    • The inequality operator != returns true if either operand is NaN (§15.21.1).

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

    If you check Double#equals method it has two exceptions

    also has the value true. However, there are two exceptions:

    • If d1 and d2 both represent Double.NaN, then the equals method returns true, even though Double.NaN==Double.NaN has the value false.

    • If d1 represents +0.0 while d2 represents -0.0, or vice versa, the equal test has the value false, even though +0.0==-0.0 has the value true.

    This definition allows hash tables to operate properly.

提交回复
热议问题