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[].
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[].
Yes, the type of a Java array is covariantly linked to its element type. Specifically, Object[] is a supertype of Integer[] and as such is not assignment-compatible with it. You must create an Integer[] at the outset to be able to assign it to an Integer[]-typed variable. From your posted code I can see no reason why you would not do that.
java - Making an Integer array inside Object class reference variable - Stack Overflow
java - int[] and Integer[] arrays - What is the difference? - Stack Overflow
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.comTwo dimensional ArrayList, need help with constructor
Videos
You can use Integer.valueOf.
Integer.valueOf((String) array [i])
The Integer class has a method valueOf which takes a string as the value and returns a int value, you can use this. It will throw an NumberFormatException if the string passed to it is not a valid integer value.
Also If you are using java5 or higher you can try using generics to make the code more readable.
You can implement the same using Generics, which would be easier.
List<Integer> numbers = new ArrayList<Integer> ();
Integer[] array = numbers.toArray (new Integer [10]);
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.
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.
There is a difference at run-time.
int[] is an array of primitive int values. Integer[] is an "object" array, holding references to Integer objects.
Most important practical difference: int[] cannot hold null values.
But I'm still confused: does
int[]store just a primitive values? If so - doesn't it mean that primitive types can live on heap without being wrapped?
int[] does store primitive types. And the array itself lives on the heap. However, those primitives are allocated as part of the array. They are not stored separately elsewhere on the heap. This is very similar to how a primitive field is part of an object instance: The object is on the heap, and its field is an integral part of that object (whereas for a non-primitive field, only the reference is stored inside the object and the target instance that reference points at is stored separately on the heap).
You could say the int is "wrapped" inside the array.
This image should help you to understand the difference:

int is a number, it's a primitive type.
Integer is an object.
When you have an array of Integers, you actually have an array of objects. Array of ints is an array of primitive types.
Since arrays are objects, they're allocated on the heap. If it's an array of ints, these ints will be allocated on the heap too, within the array.
You may find this link helpful.
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).
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.
There are two things you have to take into account here:
-
Overhead for the data itself (int vs. Integer)
-
Overhead for the object reference
So, let's start :)
-
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:
-
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:
-
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.
-
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.