Why aren't Integers cached in Java?

前端 未结 15 1501
無奈伤痛
無奈伤痛 2020-11-27 05:27

I know there are similar posts on the topic, but they don\'t quite address my question. When you do:

Integer a = 10;
Integer b = 10;
System.out.println(\"a =         


        
15条回答
  •  有刺的猬
    2020-11-27 05:48

    A new instance is a new instance, so they are equal in value, but they are not equal as objects.

    So a == b can't return true.

    If they were 1 object, as you ask for: a+=2; would add 2 to all int = 10 - that would be awful.

提交回复
热议问题