3

In the below example, how many number of objects will be created? And also Explain the logic regarding the same?

class test {
    public static void main(String args[]) {

        // 1 Integer Object
        Integer i=10;

        // 1 Integer Object
        Integer j=10;

        // sum of Integer Object
        Integer k=i+j;
    }
}

As per my knowledge it will create 2 objects. 1st for Integer i=10 it internally converts to Integer.valueOf(10) and then calls the valueof method of Integer and these internally calls the IntegerCache and gets the object by creation of it and stores in cache. similar for j as already it is cached it points to same object then the k object will be created. So total 2 .

But some are saying that Integer values with in -127 to +128 we will get objects from cache. 1st for Integer i=10 it internally converts to Integer.valueOf(10) and then calls the valueof method of Integer and these internally calls the IntegerCache and gets the object by cache. similar for j from cache. and K value 20 also from cache. So objects will be zero.

So I am not sure whether it is 0 or 2.

If anyone knows please let me know.

2 Answers 2

4

Zero is correct, all of those Integers will be pulled from the cache.

If you changed it to new Integer(10) then it would create a new object.

If you changed i, j and/or k so that they were not cached values then again it would create new objects.

Well, actually its a little more complicated than that. It's down to the JVM implementation whether the cache is created already populated or not or populated lazily on demand. From the point of view of your code though it is impossible to tell the difference and it makes no difference.

Sign up to request clarification or add additional context in comments.

3 Comments

That means Already JVM contains the cache objects ranging from -127 to 128 for Integers.. Am i correct?\
@user3729324 I just added a caveat at the end. Different JVMs are allowed to handle that differently I believe
seems jvm certainly caches Integers from -127 to 128 (inclusively), though this cant be said for the values outside this range.
1

But some are saying that Integer values with in -127 to +128 we will get objects from cache. 1st for Integer i=10 it internally converts to Integer.valueOf(10) and then calls the valueof method of Integer and these internally calls the IntegerCache and gets the object by cache. similar for j from cache. and K value 20 also from cache. So objects will be zero.

That's true, it does cache: see the ref :Integer valueOf(int i)

and if you go in the source you can find:

 /**
     * Returns an {@code Integer} instance representing the specified
     * {@code int} value.  If a new {@code Integer} instance is not
     * required, this method should generally be used in preference to
     * the constructor {@link #Integer(int)}, as this method is likely
     * to yield significantly better space and time performance by
     * caching frequently requested values.
     *
     * This method will always cache values in the range -128 to 127,
     * inclusive, and may cache other values outside of this range.
     *
     * @param  i an {@code int} value.
     * @return an {@code Integer} instance representing {@code i}.
     * @since  1.5
     */
    public static Integer valueOf(int i) {
        assert IntegerCache.high >= 127;
        if (i >= IntegerCache.low && i <= IntegerCache.high)
            return IntegerCache.cache[i + (-IntegerCache.low)];
        return new Integer(i);
    }

hope you got your answer now.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.