Mahesh
Mahesh

Reputation: 41

How many Number of objects Created

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.

Upvotes: 3

Views: 205

Answers (2)

Shailesh Aswal
Shailesh Aswal

Reputation: 6802

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.

Upvotes: 1

Tim B
Tim B

Reputation: 41208

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.

Upvotes: 4

Related Questions