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.