No, you can't cast a value which is actually a reference to an instance of Object[] to an Integer[] variable - and that's a good thing. Imagine if that were valid... consider:

Object[] values = { new Integer(5), new Integer(10) };
Integer[] integers = values;
Integer x = integers[0]; // Okay so far

values[0] = new Object(); // Sneaky!
Integer y = integers[0]; // ??? This would have to fail!

If you want to cast something to Integer[], it has to actually be an Integer[]. So this line:

final Object[] srcArray = {Integer.valueOf(1), Integer.valueOf(2)};

... needs to change to create an instance of Integer[].

Answer from Jon Skeet on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › java › arrays-in-java
Arrays in Java - GeeksforGeeks
The primitive array stores integer values and is traversed using a loop. The non-primitive array stores String objects and is printed in the same way using its length property. ... In Java, an array is declared by specifying the data type, followed ...
Published   May 8, 2026
Discussions

java - Making an Integer array inside Object class reference variable - Stack Overflow
Every single Object-type in Java inherits from the Object class. So, basically: an Integer is an Object, which is why you can do this: ... In the first example, the Integers are the Objects, in your second, the Object o is a reference to the Array of Integers More on stackoverflow.com
🌐 stackoverflow.com
java - int[] and Integer[] arrays - What is the difference? - Stack Overflow
So, an array of primitive types ... integer objects. ... Than there is a difference between Integer and int summarized here. ... Find the answer to your question by asking. Ask question ... See similar questions with these tags. ... Paging Charity! How can engineering leaders avoid becoming Bond... Why intent prediction needs more than an... ... 4 I have the following two diffetent ways of declaring an integer array in Java, one works ... More on stackoverflow.com
🌐 stackoverflow.com
int array memory size

A primitive int obviously takes 4 byte.

An Integer object has an overhead of about 24 byte (this is implementation specific) plus 4 byte for the data, so about 28 byte.

An array is an object which also has an overhead of 24 bytes plus 4 bytes for the length plus data.

An int[] array thus uses 28 bytes plus 4 bytes for each int.

An Integer[] array uses 28 bytes plus 4-8 bytes to reference each Integer object plus 28 byte for each unique Integer object. In the worst case with uncached and unique Integer objects, this equals to 28 bytes array overhead plus 36 bytes for each value.

More on reddit.com
🌐 r/javahelp
5
3
July 16, 2018
Two dimensional ArrayList, need help with constructor
There are probably better ways to solve your problem, but here is the answer to your question: List> list1 = new ArrayList>(); for (int i = 0; i < 10; i++) { List list2 = new ArrayList(); for (int j = 0; j < 10; j++) { list2.add(i * j); } list1.add(list2); } Integer value = list1.get(1).get(5); More on reddit.com
🌐 r/java
5
0
February 17, 2012
🌐
Oracle
docs.oracle.com › javase › specs › jls › se7 › html › jls-10.html
Chapter 10. Arrays
March 16, 2026 - The result of the comparison of the Class objects in the first println demonstrates that all arrays whose components are of type int are instances of the same array type, which is int[]. In the Java programming language, unlike C, an array of char is not a String, and neither a String nor an array of char is terminated by '\u0000' (the NUL character).
🌐
TutorialKart
tutorialkart.com › java › java-array › java-array-of-integers
Java Int Array
November 23, 2020 - Java Integer Array is a Java Array that contains integers as its elements.
🌐
TutorialsPoint
tutorialspoint.com › article › how-to-convert-an-object-array-to-an-integer-array-in-java
How to convert an object array to an integer array in Java?
July 30, 2019 - Contents of the integer array: [21, 58, 69, 33, 65] ... import java.util.Arrays; public class ObjectArrayToStringArray { public static void main(String args[]){ Object[] objArray = {21, 58, 69, 33, 65}; int length = objArray.length; Integer[] intArray = Arrays.copyOf(objArray, length, Integer[].class); System.out.println("Contents of the integer array: "+Arrays.toString(intArray)); } }
Top answer
1 of 4
4

Every single Object-type in Java inherits from the Object class.

So, basically: an Integer is an Object, which is why you can do this:

Object[] o = new Integer[]{1,2,3};

On the other hand, Arrays are Objects, too, meaning you can do this:

Object o = new Integer[]{1,2,3};

In the first example, the Integers are the Objects, in your second, the Object o is a reference to the Array of Integers

UPDATE: The reason between your A and B classes, you do have an Exception, is because even though each B is an A, the Array in which you store your B's is not an A.

2 of 4
2

Java arrays are covariant. Meaning that, You can use a Sub type in place of Type.

So if you have an array of "Type", you can actually fill that array with "SubType"'s. Well, any class in Java is a Subtype of Object. Hence no error in that case.

  Object o = new Integer[]{1,2,3};

It doesn't give compile fail.

Again the same things, as Array is also an Object in the end, hence you are free to assign that to an Object.

I want to know that can we iterate through the Integers in reference 'o' ?

By default, Object is not iterable. Where as Array object is.

So before you going to iterate, you have to cast it to type Array.

Update :

But her A a = new B[4]; gives CF

Ofcourse that is not a valid declaration You should write

A[] a = new B[4];  // just to satisfy the compiler. At run time you are not allowed to store A's in it.

But if you are trying to achive the style

  Object o = new Integer[]{1,2,3};

No that won't work here and you can only write

 Object o =  new B[4];

That is because array is a sub type of Object class and not A class.

Find elsewhere
🌐
Baeldung
baeldung.com › home › java › java array › convert an array of primitives to an array of objects
Convert an Array of Primitives to an Array of Objects | Baeldung
June 14, 2025 - Let’s say we have an array of primitives, such as int[], and we would like to convert it to an array of objects, Integer[]. We might intuitively try casting: Integer[] integers = (Integer[])(new int[]{0,1,2,3,4}); However, this will result ...
🌐
Quora
quora.com › In-Java-what-is-an-integer-array
In Java, what is an integer array? - Quora
Answer (1 of 7): It depends on whether you are referring to [code ]int[/code] arrays or [code ]Integer[/code] arrays. While both hold a list of integers, the first holds the integers as 32-bit primitives- meaning that they are not stored as objects, but rather as numbers. The latter, however, hol...
🌐
Coderanch
coderanch.com › t › 386373 › java › Cast-Object-int
Cast Object to int[] (Java in General forum at Coderanch)
Yes, it's possible, but only if the object actually is an int[]. In other words, this is legal: Object o = new int[5]; int[] ia = (int[]) o; When casting object references, the cast never makes any change to the object itself; it's only an instruction to the compiler to treat the object as ...
🌐
Mkyong
mkyong.com › home › java › java – convert int[] to integer[] example
Java - Convert int[] to Integer[] example - Mkyong.com
February 22, 2014 - package com.mkyong.test; public class ArrayConvertExample { public static void main(String[] args) { int[] obj = new int[] { 1, 2, 3 }; Integer[] newObj = toObject(obj); System.out.println("Test toObject() - int -> Integer"); for (Integer temp : newObj) { System.out.println(temp); } Integer[] obj2 = new Integer[] { 4, 5, 6 }; int[] newObj2 = toPrimitive(obj2); System.out.println("Test toPrimitive() - Integer -> int"); for (int temp : newObj2) { System.out.println(temp); } } // Convert int[] to Integer[] public static Integer[] toObject(int[] intArray) { Integer[] result = new Integer[intArray.
🌐
javathinking
javathinking.com › blog › convert-obj-array-to-int-array-java
Converting Object Array to Integer Array in Java — javathinking.com
An int[] is a primitive array in Java that can only hold integer values. Primitive arrays are more memory-efficient than arrays of wrapper classes (Integer[]) because they store the actual values directly rather than references to objects.
🌐
Blogger
javarevisited.blogspot.com › 2013 › 05 › how-to-convert-list-of-integers-to-int-array-java-example-tips.html
How to Convert List of Integers to Int Array in Java - Example
June 11, 2025 - Though it sound simple, there is no straight forward way to convert a list of Integer object to int array in Java. Only way to do this conversion is by using Java 1.5 foreach loop or ArrayUtils from Apache commons.
🌐
Runestone Academy
runestone.academy › ns › books › published › apcsareview › ArrayBasics › abasics.html
8.1. Arrays in Java — AP CSA Java Review - Obsolete
Array elements are initialized to 0 if they are a numeric type (int or double), false if they are of type boolean, or null if they are an object type like String. Figure 2: Two 5 element arrays with their values set to the default values for integer and object arrays.¶
🌐
Igmguru
igmguru.com › blog › arrays-in-java
Arrays in Java: Declare, Define, and Access Array
May 11, 2026 - Each value is accessed by referring to its specific index within the array. Here is a Java program that tells you how to create an integer array, assign values to it and display each element on the console. An array of objects is a structure that holds references to many objects of the same ...
🌐
Reddit
reddit.com › r/javahelp › int array memory size
r/javahelp on Reddit: int array memory size
July 16, 2018 -

How many bytes does an int[] vs. Integer[] be in memory? Like including the overhead and extra data required, not just the data itself? I know it has an additional length which is an int (8 bytes).

Top answer
1 of 3
4

A primitive int obviously takes 4 byte.

An Integer object has an overhead of about 24 byte (this is implementation specific) plus 4 byte for the data, so about 28 byte.

An array is an object which also has an overhead of 24 bytes plus 4 bytes for the length plus data.

An int[] array thus uses 28 bytes plus 4 bytes for each int.

An Integer[] array uses 28 bytes plus 4-8 bytes to reference each Integer object plus 28 byte for each unique Integer object. In the worst case with uncached and unique Integer objects, this equals to 28 bytes array overhead plus 36 bytes for each value.

2 of 3
2

There are two things you have to take into account here:

  1. Overhead for the data itself (int vs. Integer)

  2. Overhead for the object reference

So, let's start :)

  1. Integer vs. int.

Primitive int is just a 4 bytes value (not 8 bytes as you've written). Integer is an object, which consists of a primitive int value (4 bytes) and object header. Object header can be 8, 12, or 16 bytes depending on the bitness of your JVM and UseCompressedOops flag. Here is Stackoverflow answer that describes the structure of the object header: https://stackoverflow.com/questions/26357186/what-is-in-java-object-header

UseCompressedOops is a flag that makes JVM optimize most of the references on 64-bit JVM and make them take 32 bits instead of 64 bits. If set, it should also make object header take 12 bytes instead of 16 bytes. (To be honest, I didn't measure this).

Given that, the overhead for a bunch of integers will be

number_of_elements * object_header_overhead

, where object_header_overhead =

  • 8 bytes (on 32 bit JVM)

  • 12 bytes (on 64 bit JVM with UseCompressedOops = true)

  • 16 bytes (on 64 bit without compressed oops)

But that's not the end of the story.

2. Size of object reference

int is a primitive, in another words it's just 4 bytes of data, and that's it. Integer is an object, which means it should be referenced from some another place in your program. If it is not referenced from anywhere, it's lost (and soon will be eaten by the GC).

Now, if you have an array of ints, then each element of it is a 4 byte int value. If you have an array of Integers, each element of it will be not Integer, but a reference to Integer object.

The size of an object reference is also can be different on different JVMs:

- 32 bits on 32 bit JVM

- 64 bits on 64 bit JVM

- 32 bits on 64 bit JVM with UseCompressedOops

Now you can calculate total overhead for your specific case. Here are few examples:

  1. Say, you run the following code on 32 bit JVM:

i1 = new Integer[100];
i2 = new int[100];

These are just empty arrays. The memory taken by them will be exactly the same, because each element of each array will take 4 bytes (0 int is 4 bytes, and null is 32 bits on 32 bit JVM).

2. Now, say you have 64 bit JVM without compressed oops.

i1 = new Integer[100];
i2 = new int[100];

for (int i = 0; i < 100; i++) {
  i1[i] = i; // implicit boxing: new Integer(i);
  i2[i] = i;
}

Now the memory taken by a single element of Integer array will consist of:

  • 4 bytes int value

  • 16 bytes - object header

  • 8 bytes object reference,

while an element of int array will only take 4 bytes. In total you have (16 + 8) * 100 = 240 bytes.

3. Of course, you can have more complicated situations, for instance (let's say, we have 64 bit JVM again):

i1 = new Integer[100];
i2 = new int[100];

int primInt = 42;
Integer wrappedInt = new Integer(42);

for (int i = 0; i < 100; i++) {
  i1[i] = wrappedInt;
  i2[i] = primInt;
}

As you can see, now every element of Integer array references a single Integer object. So, the total memory overhead will consist of:

  • 100 * 4 bytes (4 bytes for primitive int vs. 8 bytes for object reference on 64 bit JVM)

  • 20 bytes (size of the single Integer object, consisting of 16 bytes for the header and 4 bytes for the int value).

And couple more things to add:

  1. I don't guarantee that the information about the size of object header is 100% correct. This is what I remember about it, so please verify it yourself if you need to.

  2. Things can get even more complicated, because Java tends to cache Integers and reuse them. So actual memory of your programs can be less than expected.

🌐
IIT Kanpur
iitk.ac.in › esc101 › 05Aug › tutorial › java › data › arraysOfObjects.html
Arrays of Objects
Integer[] anArray = new Integer[5]; This brings us to a potential stumbling block, often encountered by new programmers, when using arrays that contain objects. After the previous line of code is executed, the array called anArray exists and has enough room to hold five integer objects.
🌐
BeginnersBook
beginnersbook.com › 2022 › 09 › convert-integer-list-to-int-array-in-java
Convert Integer List to int Array in Java
September 23, 2022 - Convert Stream<Integer> to int array using toArray() method. import java.util.*; class JavaExample { public static void main(String[] args) { // List of Integer type List<Integer> arrList = new ArrayList<>(); arrList.add(1); arrList.add(3); arrList.add(5); arrList.add(7); arrList.add(9); ...