What exactly does comparing Integers with == do?

前端 未结 7 557
旧时难觅i
旧时难觅i 2020-12-10 04:22

EDIT: OK, OK, I misread. I\'m not comparing an int to an Integer. Duly noted.

My SCJP book says:

When == is used to compare a primitive to a

7条回答
  •  不思量自难忘°
    2020-12-10 05:16

    Since Java 5.0, there is automatic boxing and unboxing, meaning that wrappers can be implicitly converted to primitives and vice versa. However, if you compare two Integer objects, you are still comparing two references, and there is nothing that would trigger automatic boxing/unboxing. If that was the case, code written in J2SE 1.4 and prior would break.

提交回复
热议问题