Why aren't Integers cached in Java?

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

    In Java, every time you call the new operator, you allocate new memory and you create a new object. That's standard language behavior, and to my knowledge there is no way to bypass this behavior. Even standard classes have to abide by this rule.

    0 讨论(0)
  • 2020-11-27 05:55

    It is my understanding that new will create a new object, no matter what. The order of operations here is that you first call new, which instantiates a new object, then the constructor gets called. There is no place for the JVM to intervene and turn the new into a "grab a cached Integer object based on the value passed into the constructor".

    Btw, have you considered Integer.valueOf? That works.

    0 讨论(0)
  • 2020-11-27 05:58

    Let me just expand slightly on ChrisJ's and EboMike's answers by giving links to the relevant sections of the JLS.

    new is a keyword in Java, allowed in class instance creation expressions (Section 15.9 of the JLS). This is different from C++, where new is an operator and can be overloaded.

    The expression always tries to allocate memory, and yields a fresh object each time it is evaluated (Section 15.9.4). So at that point it's already too late for cache lookup.

    0 讨论(0)
  • 2020-11-27 05:59

    This would potentially break code written before this design change, when everybody righfully assumed that two newly created instances were different instances. It could be done for autoboxing, because autoboxing didn't exist before, but changing the meaning of new is too dangerous, and probably doesn't bring much gain. The cost of short-lived objects is not big in Java, and could even be lower than the cost of maintaining a cache of long-lived objects.

    0 讨论(0)
  • 2020-11-27 05:59

    For Integer objects use the a.equals(b) condition to compare.

    The compiler will not do the unboxing for you while you compare, unless you assign the value to a basic type.

    0 讨论(0)
  • 2020-11-27 06:02

    Assuming your describing the behavior of you code accurately it sounds like autoboxing isn't working on the 'gets' (=) operatior, instead it sounds like Integer x = 10; gives the object x a memory pointer of '10' instead of a vale of 10. Therefore ((a == b) == true)( will evaluate to true because == on objects operates on the memory addresses which you assigned both to 10.

    So when should you use autoboxing and unboxing? Use them only when there is an “impedance mismatch” between reference types and primitives, for example, when you have to put numerical values into a collection. It is not appropriate to use autoboxing and unboxing for scientific computing, or other performance-sensitive numerical code. An Integer is not a substitute for an int; autoboxing and unboxing blur the distinction between primitive types and reference types, but they do not eliminate it.

    What oracle has to say on the subject.

    Notice that the documentation doesn't supply any examples with the '=' operator.

    0 讨论(0)
提交回复
热议问题