<p>An array of ints in java is stored as a block of 32-bit values in memory. How is an array of Integer objects stored? i.e.</p>

<pre><code>int[] vs. Integer[]
</code></pre>

<p>I'd imagine that each element in the Integer array is a reference to an Integer object, and that the Integer object has object storage overheads, just like any other object.</p>

<p>I'm hoping however that the JVM does some magical cleverness under the hood given that Integers are immutable and stores it just like an array of ints.</p>

<p>Is my hope woefully naive? Is an Integer array much slower than an int array in an application where every last ounce of performance matters?</p>

[{'answer_id': 76588, 'author': 'Will Hartung', 'author_id': 13663, 'author_profile': 'https://Stackoverflow.com/users/13663', 'pm_score': 1, 'selected': False, 'text': '<p>I think your hope is woefully naive. Specifically, it needs to deal with the issue that Integer can potentially be null, whereas int can not be. That alone is reason enough to store the object pointer.</p>\n\n<p>That said, the actual object pointer will be to a immutable int instance, notably for a select subset of integers.</p>\n'}, {'answer_id': 76596, 'author': 'cynicalman', 'author_id': 410, 'author_profile': 'https://Stackoverflow.com/users/410', 'pm_score': 0, 'selected': False, 'text': '<p>It won\'t be much slower, but because an Integer[] must accept "null" as an entry and int[] doesn\'t have to, there will be some amount of bookkeeping involved, even if Integer[] is backed by an int[].</p>\n\n<p>So if every last ounce of performance matters, user int[]</p>\n'}, {'answer_id': 76720, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 0, 'selected': False, 'text': "<p>The reason that Integer can be null, whereas int cannot, is because Integer is a full-fledged Java object, with all of the overhead that includes. There's value in this since you can write</p>\n\n<pre><code>Integer foo = new Integer();\nfoo = null; \n</code></pre>\n\n<p>which is good for saying that foo will have a value, but it doesn't yet. </p>\n\n<p>Another difference is that <code>int</code> performs no overflow calculation. For instance,</p>\n\n<pre><code>int bar = Integer.MAX_VALUE;\nbar++;\n</code></pre>\n\n<p>will merrily increment bar and you end up with a very negative number, which is probably not what you intended in the first place.</p>\n\n<pre><code>foo = Integer.MAX_VALUE;\nfoo++;\n</code></pre>\n\n<p>will complain, which I think would be better behavior. </p>\n\n<p>One last point is that Integer, being a Java object, carries with it the space overhead of an object. I think that someone else may need to chime in here, but I believe that every object consumes 12 bytes for overhead, and then the space for the data storage itself. If you're after performance and space, I wonder whether Integer is the right solution.</p>\n"}, {'answer_id': 80220, 'author': 'stepancheg', 'author_id': 15018, 'author_profile': 'https://Stackoverflow.com/users/15018', 'pm_score': 2, 'selected': False, 'text': '<p>John Rose working on <a href="http://blogs.oracle.com/jrose/entry/fixnums_in_the_vm" rel="nofollow noreferrer">fixnums</a> in the JVM to fix this problem.</p>\n'}, {'answer_id': 80718, 'author': 'ralfs', 'author_id': 13107, 'author_profile': 'https://Stackoverflow.com/users/13107', 'pm_score': 5, 'selected': True, 'text': "<p>No VM I know of will store an Integer[] array like an int[] array for the following reasons:</p>\n\n<ol>\n<li>There can be <strong>null</strong> Integer objects in the array and you have no bits left for indicating this in an int array. The VM could store this 1-bit information per array slot in a hiden bit-array though.</li>\n<li>You can synchronize in the elements of an Integer array. This is much harder to overcome as the first point, since you would have to store a monitor object for each array slot.</li>\n<li>The elements of Integer[] can be compared for identity. You could for example create two Integer objects with the value 1 via <strong>new</strong> and store them in different array slots and later you retrieve them and compare them via ==. This must lead to false, so you would have to store this information somewhere. Or you keep a reference to one of the Integer objects somewhere and use this for comparison and you have to make sure one of the == comparisons is false and one true. This means the whole concept of object identity is quiet hard to handle for the <em>optimized</em> Integer array.</li>\n<li>You can cast an Integer[] to e.g. Object[] and pass it to methods expecting just an Object[]. This means all the code which handles Object[] must now be able to handle the special Integer[] object too, making it slower and larger.</li>\n</ol>\n\n<p>Taking all this into account, it would probably be possible to make a special Integer[] which saves some space in comparison to a <em>naive</em> implementation, but the additional complexity will likely affect a lot of other code, making it slower in the end.</p>\n\n<p>The overhead of using Integer[] instead of int[] can be quiet large in space and time. On a typical 32 bit VM an Integer object will consume 16 byte (8 byte for the object header, 4 for the payload and 4 additional bytes for alignment) while the Integer[] uses as much space as int[]. In 64 bit VMs (using 64bit pointers, which is not always the case) an Integer object will consume 24 byte (16 for the header, 4 for the payload and 4 for alignment). In addition a slot in the Integer[] will use 8 byte instead of 4 as in the int[]. This means you can expect an overhead of <strong>16 to 28</strong> byte per slot, which is a <strong>factor of 4 to 7</strong> compared to plain int arrays.</p>\n\n<p>The performance overhead can be significant too for mainly two reasons:</p>\n\n<ol>\n<li>Since you use more memory, you put on much more pressure on the memory subsystem, making it more likely to have cache misses in the case of Integer[]. For example if you traverse the contents of the int[] in a linear manner, the cache will have most of the entries already fetched when you need them (since the layout is linear too). But in case of the Integer array, the Integer objects itself might be scattered randomly in the heap, making it hard for the cache to guess where the next memory reference will point to.</li>\n<li>The garbage collection has to do much more work because of the additional memory used and because it has to scan and move each Integer object separately, while in the case of int[] it is just one object and the contents of the object doesn't have to be scanned (they contain no reference to other objects).</li>\n</ol>\n\n<p>To sum it up, using an int[] in performance critical work will be both much faster and memory efficient than using an Integer array in current VMs and it is unlikely this will change much in the near future.</p>\n"}]
['https://Stackoverflow.com/questions/76549', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/974/']