Why are these == but not `equals()`?

后端 未结 8 1697
灰色年华
灰色年华 2020-12-01 07:58

I\'m a bit confused about the way Java treats == and equals() when it comes to int, Integer and other types of numbers.

8条回答
  •  天涯浪人
    2020-12-01 08:52

    The morale of the story:

    Autoboxing/unboxing is confusing, as is type promotion. Together, they make for good riddles but horrendous code.

    In practice, it seldom makes sense to use numeric types smaller than int, and I'm almost inclined to configure my eclipse compiler to flag all autoboxing and -unboxing as an error.

提交回复
热议问题