Good day. I read about autopacking in Java and the question appeared:

Integer i0 = 100; Integer i1 = new Integer(100); System.out.println(i0 == i1); 

Why is the result false? After all, if you do this:

  Integer i0 = 100; Integer i1 = 100; System.out.println(i0 == i1); 

That result will be true. Although in the first example the line:

  Integer i1 = new Integer(100); 

Where the value of 100 is also packed, but only manually, and not automatically (as in the second example) into an object of type Integer.

    2 answers 2

    Such code

     Integer i0 = 100; 

    equivalent to this:

     Integer i0 = Integer.valueOf(100); 

    If you look at the implementation of the valueOf method, you can see that for values ​​from -128 to 127, the cache of objects of type Integer . See also the documentation for the valueOf method.

    When the constructor is called, the cache is not used and a new instance of the Integer class is created.

    • Say, but the line System.out.println (i0 == i1) does not check the equality of i0 and i1, but checks whether the references of these objects indicate the same memory area. Then as in the second example, the result can be true? - Drylozav
    • one
      @Drylozav: because in this case it is called 2 times by valueOf , which returns the same - cached after all! - an object. - VladD
    • I do not really understand at all what it means to “return a cached object”? What happens to the object when it is entered into the cache? - Drylozav
    • one
      @Drylozav Nothing happens to him :) Just use the same object so as not to allocate memory for it, and then not release it. Optimization such. See definition of ru.wikipedia.org/wiki/%D0%9A%D1%8D%D1%88 - a_gura
    • one
      @Drylozav: Because creating an object is a slightly slower operation than fetching a ready-made array. Since the implicit boxing of small numbers is a very, very frequent operation in Java, this optimization is justified. - VladD

    Well, the first example of why false is understandable, since references to different objects are compared.

    In the second case, the idea would have to be false too, but the problem is that auto-objects are cached and the second call i1=100 will return the object previously created with i0=100 , so the comparison returns true

    In general, such things to do with autoboxing is not recommended. Autoboxing was not invented for this.

    • and for what ? Actually, I still do not understand why the idea of ​​caching objects was invented at all? And why in the range of -128 ... 127? Is this some kind of "crutch" of developers? - Drylozav
    • @Drylozav This range can be customized to your liking (but not less than the default). This is not a crutch, this is optimization. First of all, memory consumption is optimized, and, as a result, performance. It is assumed that small integers are very often used by developers compared to large ones, and therefore they decided to cache them. - a_gura
    • @Drylozav This caching is a certain kind of optimization to save in such cases as for loops. -128 ... 127 is the byte range. @a_gura, where did you see this setting? ;-) - JEcho
    • one
      @JEcho I quote raw docks /** * Cache to support the object identity semantics of autoboxing for values between * -128 and 127 (inclusive) as required by JLS. * * The cache is initialized on first usage. The size of the cache * may be controlled by the -XX:AutoBoxCacheMax=<size> option. * During VM initialization, java.lang.Integer.IntegerCache.high property * may be set and saved in the private system properties in the * sun.misc.VM class. */ /** * Cache to support the object identity semantics of autoboxing for values between * -128 and 127 (inclusive) as required by JLS. * * The cache is initialized on first usage. The size of the cache * may be controlled by the -XX:AutoBoxCacheMax=<size> option. * During VM initialization, java.lang.Integer.IntegerCache.high property * may be set and saved in the private system properties in the * sun.misc.VM class. */ /** * Cache to support the object identity semantics of autoboxing for values between * -128 and 127 (inclusive) as required by JLS. * * The cache is initialized on first usage. The size of the cache * may be controlled by the -XX:AutoBoxCacheMax=<size> option. * During VM initialization, java.lang.Integer.IntegerCache.high property * may be set and saved in the private system properties in the * sun.misc.VM class. */ - a_gura