Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

In implementation of a generic stack ,the following idiom is used and works without any problem

public class GenericStack<Item> {
    private int N;
    private Item[] data;    

    public GenericStack(int sz) {
        super();
        data = (Item[]) new Object[sz];

    }
        ...
}

However when I try the following ,it causes a ClassCastException

String[] stra = (String[]) new Object[4];

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

How do you explain this?

share|improve this question
    
String doesn't inherit from Item, thus the cast is not possible. –  Piovezan Jun 14 '13 at 12:33
    
@Piovezan String and Item are not in the same line. –  Dukeling Jun 14 '13 at 12:34
    
new GenericStack<String>(10) works but if you try String[] stra = new GenericStack<String>(10).getData(); (implement the corresponding getter), it fails with the ClassCastException. So it does not "really" work, the cast has not been done magically. –  Arnaud Denoyelle Jun 14 '13 at 12:38
    
That said, you can still use Array.newInstance(Class clazz, Integer) to generically create your array. Example Array.newInstance(String.class, sz). –  Arnaud Denoyelle Jun 14 '13 at 12:41
    
Item is not a class, it is just the generic type name. Generics are pretty specific in this way. If you want to answer it, I would focus on finding out when generic types are being substituted with actual type if I were you. It is certainly not compile time –  Jan Hruby Jun 14 '13 at 12:41

4 Answers 4

up vote 3 down vote accepted

Casting a new Object[4] to a String[] doesn't work because an Object[] isn't a String[], just like an Object isn't a String.

The first example works because of type erasure. At runtime, the type parameterItem has been erased to Object. However it would similarly fail if you tried to assign the array to a reifiable type, for example if data wasn't private:

String[] strings = new GenericStack<String>(42).data;

This would similarly throw a ClassCastException, because what is actually an Object[] would be cast to String[].

share|improve this answer

Because of generics type erasure Item array becomes Object array effectivelly. Therefore type matches. But when you do it with concrete type String it does not. No type erasure is applied and it fails.

share|improve this answer

I think if a Object class reference is pointing to any child class object like String class or any other class then only we can cast the object class reference to that particular class (which object we have created and assigned to Object class ref) and assign a reference to it. and if we create a direct object of Object class (as you have mentioned) will not work.

Example:

Object[] obj = new String[4];
String[] stra = (String[]) obj;

above code will not generate any ClassCastException.

Hope this helps

share|improve this answer

I think this is the best answer to this question:

"A String[] is not an Object[], which is a little counter-intuitive. You're encountering the "Banana is-a fruit" but a "List Of Bananas is-not-a List Of Fruit" scenario."

Ref: http://stackoverflow.com/a/1018774/847818

From the same question, how to cast:

String[] stringArray = Arrays.copyOf(objectArray, objectArray.length, String[].class);

or

String[] stringArray = Arrays.asList(objectArray).toArray(new String[objectArray.length]);
share|improve this answer
2  
The correct action here may've been simply a comment on the question saying something like "Possible duplicate". –  Dukeling Jun 14 '13 at 13:07
    
Possibly, but personally if the answer is not really the same I would wait for a feedback from the OP. Maybe he will ask for something more and different. :) –  Enrichman Jun 14 '13 at 13:18
    
thanks @Enrichman for the link ,I got the generic stack code from sedgewick's algo book.. your link helped me understand it better –  damon Jun 14 '13 at 13:30
1  
-1 Arrays are covariant (unlike generics): a String[] is an Object[]. The ClassCastException is happening because an Object[] is not a String[]. –  Paul Bellora Jun 14 '13 at 13:41
1  
The cited answer was incorrect and has now been deleted. –  Paul Bellora Jun 14 '13 at 14:26

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.