aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes casting of an array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "casting of an array" Watch "casting of an array" New topic
Author

casting of an array

jinesh parikh
Ranch Hand

Joined: Apr 20, 2007
Posts: 48
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

Joined: Mar 19, 2007
Posts: 99

Hi jinesh parikh

The exception itself explains the reason you are getting a class cast exception. Just have a look at the exception again


MooN
jinesh parikh
Ranch Hand

Joined: Apr 20, 2007
Posts: 48
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

Joined: Mar 19, 2007
Posts: 99

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

Joined: Jul 02, 2007
Posts: 29
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.


-- deepak <br />SCJP 5.0, SCWCD 5.0
jinesh parikh
Ranch Hand

Joined: Apr 20, 2007
Posts: 48
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

Joined: Aug 06, 2007
Posts: 27
Hi Jinesh.
Please go thru Kathy Bert - SCJP 5 book. Chapter 3--> topic Array Declaration --> subtopic Legal Array Element Assignments onwards. thanks.


SCJP Tiger Hunter 91%.
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
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.


all events occur in real time
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: casting of an array