Why aren't Integers cached in Java?

前端 未结 15 1627
無奈伤痛
無奈伤痛 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:44

    It's because you're using the new statement to construct the objetcs.

    Integer a = Integer.valueOf(10);
    Integer b = Integer.valueOf(10);
    System.out.println("a == b: " + (a == b));
    

    That will print out true. Weird, but Java.

提交回复
热议问题