[James]: you can NOT cast Object to Integer Integer is not subtype of Object
For better or worse, yes you can cast it. Java treats Integer as a subtype of Object - even though this can create some problems, since for example you can put a String in an Object, but not in a Integer. Nonetheless, this both compiles and runs:
In general though, you can save yourself a lot of headaches by not attempting to use raw types like this.
"I'm not back." - Bill Harding, Twister
Joined: Feb 07, 2005
If the actual runtime type of the array is Object, then you can't cast it. But if the runtime type is Integer, but it is currently referred to as an Object or Number, you can cast it to an Integer.
Joined: Jan 30, 2000
[James]: still the question is why does toArray(T a) return Object, not T if generic type is not provided?
Good question. On the surface, E and T are defined independently. The answer is mostly found in JLS 15.12:
The result type of the chosen method is determined as follows: [...] if unchecked conversion was necessary for the method to be applicable then the result type is the erasure (�4.6) of the method's declared return type.
Now it's not immediately apparent that unchecked conversion was needed at the line in question. However in order to determine applicability of a method, the type of the target reference must be determined. And in this case, that required unchecked conversion from List<E> to List. Basically, once you use a raw type somewhere, it tends to pervasively erase types on anything it touches.
Joined: Oct 02, 2006
ok, then. a little bit off topic: If Integer is the subtype of Object, what are the exact class types of Integer and Object? Is an array supposed to be Object type? If so, why Integer cannot be cast to String at compile time?
Joined: Jan 30, 2000
[James]: a little bit off topic: If Integer is the subtype of Object, what are the exact class types of Integer and Object?
They're Integer and Object - exactly that.
[James]: Is an array supposed to be Object type?
All arrays are subtypes of Object, yes.
[James]: If so, why Integer cannot be cast to String at compile time?
For much the same reason that an Integer cannot be cast to a String at compile time. I'll discuss that first because it's simpler and more likely to be familiar. An Object reference can be cast to either Integer or String, because the compiler knows that a variable of reference type Object could hold a reference to a subtype, such as Integer or String. So the compiler will insert the cast, and at runtime the JVM will check if it's valid or not, based on the actual type of the object referenced by the variable. However, a variable of type Integer could not possibly hold a reference to a String - so if you try to cast it, the compiler will simply give you an error right away, rather than waiting until runtime. Although Integer and String are both subtypes of Object, they are otherwise unrelated - and neither one is a subtype of the other. An Integer is not a String, and a String is not an Integer. Casting between the two is impossible.
In much the same way, Integer and String are subtypes of Object - but other than that, they are unrelated, because Integer and String are unrelated. You could take the preceding paragraph and replace every Object, Integer and String with Object, Integer and String - the logic works just the same. An Integer is not a String, and a String is not an Integer. There's no way a reference of one type could possibly refer to an instance of the other, so casting is impossible.