Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

casting of an array

 
jinesh parikh
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
consider the following code

public class JListExample {
public static void main(String[] args) {
Object temparry[]={new String("jinesh"),new String("parikh")};
String s[]=(String[])temparry;
}

}

when i run this programme it gives me the runtime exception as follows

java.lang.ClassCastException: [Ljava.lang.Object;
at List.JListExample.main(JListExample.java:81)
Exception in thread "main"

can anybody tell me why is this giving me error ?
 
Abdullah Mamun
Ranch Hand
Posts: 99
Eclipse IDE Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi jinesh parikh

The exception itself explains the reason you are getting a class cast exception. Just have a look at the exception again
 
jinesh parikh
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Al Mamun ,

i know dear that exception is telling me that i am casting wrong.But i am not getting the reason for the exception because String is a subclass of an object class.I am downcasting the object array to the String array which is perfect.Then why is this error coming?
 
Abdullah Mamun
Ranch Hand
Posts: 99
Eclipse IDE Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From the first post I actually couldn't get your problem.

In this example you are trying to cast a Base class to a Derived class which is absolutely wrong because since Derived is a type of Base, Base is not a type of Derived.


On the above line you are creating an Object array. As you know you can assign any type of objects in the Object array which is not necessarily to be a String, but you are trying to cast the Object array to String array. It is not possible to cast all the objects to String.


But if you write the line this way, you can cast the Object[] to a String[] array because you created a String array.

I hope, you understand what I tried to mean.
[ August 23, 2007: Message edited by: Al Mamun ]
 
Deepak Vadgama
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An Object array may contain elements i.e. objects which are instances of any class (Since all classes implicitly extend from Object class)

Thus JVM cannot allow an Object[] to be downcasted into String[]


On the other hand,

This will work,
because while downcasting JVM is sure now that even if objArr is Object array,it can only store Strings and not any other object (Since Object[] variable is referring to String[])

Hope it helped.
 
jinesh parikh
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
when i am fetching the individual element of the array and casting it
to a string then it is working fine.
when i am fetching individual element it is an object and in object is a superclass of all the classes it can be anything inside it.

Then why at that time java does not give me any error?
 
Ajay Chandravadiya
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jinesh.
Please go thru Kathy Bert - SCJP 5 book. Chapter 3--> topic Array Declaration --> subtopic Legal Array Element Assignments onwards. thanks.
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Howdy ranchers,




It is because this line:
Object temparry[]={new String("jinesh"),new String("parikh")};

At runtime it is not sure, that ALL the elements in this array are of type String, because it would be legal to write:
Object temparry[]={new String("jinesh"),new Integer(49) };

Therefore the downcasting only works without exception when it is like in the example with a polymorphic declaration of the array. Because it is clear than that this array holds only Strings (others would cause an ArrayStoreException if you try).

Something like
Object[] array = new Object[5];
Integer[] intis;
intis = (Integer[]) array;

would also cause a ClassCastException.

Hope that helped.



Yours,
Bu.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic