6

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?

Paul Bellora
  • 54,340
  • 18
  • 130
  • 181
damon
  • 8,127
  • 17
  • 69
  • 114
  • String doesn't inherit from Item, thus the cast is not possible. – Piovezan Jun 14 '13 at 12:33
  • 1
    @Piovezan `String` and `Item` are not in the same line. – Bernhard Barker Jun 14 '13 at 12:34
  • `new GenericStack(10)` works but if you try `String[] stra = new GenericStack(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
  • ArrayList too _Comments mut be at least 15 characters in length._ – user2336315 Jun 14 '13 at 12:48

4 Answers4

3

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[].

Paul Bellora
  • 54,340
  • 18
  • 130
  • 181
2

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.

Grzegorz Żur
  • 47,257
  • 14
  • 109
  • 105
1

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

vikasgupta
  • 325
  • 2
  • 3
  • 12
0

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: https://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]);
Community
  • 1
  • 1
Enrichman
  • 11,157
  • 11
  • 67
  • 101
  • 2
    The correct action here may've been simply a comment on the question saying something like "[Possible duplicate](http://stackoverflow.com/questions/1018750/how-to-convert-object-array-to-string-array-in-java)". – Bernhard Barker 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