72

What's wrong with the following code?

Object[] a = new Object[1];
Integer b=1;
a[0]=b;
Integer[] c = (Integer[]) a;

The code has the following error at the last line :

Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Ljava.lang.Integer;

Draken
  • 3,049
  • 13
  • 32
  • 49
Ross
  • 1,913
  • 4
  • 18
  • 17

5 Answers5

99

Ross, you can use Arrays.copyof() or Arrays.copyOfRange() too.

Integer[] integerArray = Arrays.copyOf(a, a.length, Integer[].class);
Integer[] integerArray = Arrays.copyOfRange(a, 0, a.length, Integer[].class);

Here the reason to hitting an ClassCastException is you can't treat an array of Integer as an array of Object. Integer[] is a subtype of Object[] but Object[] is not a Integer[].

And the following also will not give an ClassCastException.

Object[] a = new Integer[1];
Integer b=1;
a[0]=b;
Integer[] c = (Integer[]) a;
Sotirios Delimanolis
  • 252,278
  • 54
  • 635
  • 683
namalfernandolk
  • 8,328
  • 11
  • 53
  • 105
25

You can't cast an Object array to an Integer array. You have to loop through all elements of a and cast each one individually.

Object[] a = new Object[1];
Integer b=1;
a[0]=b;
Integer[] c = new Integer[a.length];
for(int i = 0; i < a.length; i++)
{
    c[i] = (Integer) a[i];
}

Edit: I believe the rationale behind this restriction is that when casting, the JVM wants to ensure type-safety at runtime. Since an array of Objects can be anything besides Integers, the JVM would have to do what the above code is doing anyway (look at each element individually). The language designers decided they didn't want the JVM to do that (I'm not sure why, but I'm sure it's a good reason).

However, you can cast a subtype array to a supertype array (e.g. Integer[] to Object[])!

Vladimir Vagaytsev
  • 2,650
  • 9
  • 31
  • 30
Sean
  • 4,320
  • 23
  • 21
  • 3
    The fact that arrays are covariant means that the JVM already has to check for type safety when it performs assignments - but not when it just reads an element. – Jon Skeet Jul 12 '09 at 06:27
  • 4
    The reason is pretty simple. If you were allowed to cast an Object[] to Integer[], then the JVM could never be sure of the actual type of the objects in the array, since old references to the array as an Object[] might still exist. It would have to typecheck every access of an object in an array, every time, since it would never know for sure what it was. And if the type check failed, it would throw an exception somewhere completely different from where the cause was. – Captain Ford Sep 27 '16 at 18:04
14

Or do the following:

...

  Integer[] integerArray = new Integer[integerList.size()];
  integerList.toArray(integerArray);

  return integerArray;

}
Michael
  • 2,178
  • 1
  • 21
  • 31
  • Is this integerArray which is Integer[] equal to int[] ? I guess it is not. Need to do unboxing. – Akh Aug 28 '13 at 23:06
  • 1
    Only works if _integerList_ is a List or an ArrayList.. not for "classic" Arrays tho :( – f1v3 Sep 13 '17 at 14:35
  • 1
    you could also use `Arrays.asList(objectArray).toArray(new Integer[objectArray.length])`, but then, this does exactly the same as [namalfernandolk's answer](https://stackoverflow.com/a/8220245/6723250) with unneccessary overhead – Xerus Oct 17 '17 at 15:53
5
java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Ljava.lang.Integer;

you try to cast an Array of Object to cast into Array of Integer. You cant do it. This type of downcast is not permitted.

You can make an array of Integer, and after that copy every value of the first array into second array.

thkala
  • 76,870
  • 21
  • 145
  • 185
Tommaso Taruffi
  • 8,462
  • 9
  • 40
  • 54
1

When casting is done in Java, Java compiler as well as Java run-time check whether the casting is possible or not and throws errors in case not.

When casting of Object types is involved, the instanceof test should pass in order for the assignment to go through. In your example it results
Object[] a = new Object[1]; boolean isIntegerArr = a instanceof Integer[]
If you do a sysout of the above line, it would return false;
So trying an instance of check before casting would help. So, to fix the error, you can either add 'instanceof' check
OR
use following line of code:
(Arrays.asList(a)).toArray(c);

Please do note that the above code would fail, if the Object array contains any entry that is other than Integer.

S R Chaitanya
  • 570
  • 4
  • 10