permaculture playing cards*
The moose likes Beginning Java and the fly likes Can I caste Object[] to String[]? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Can I caste Object[] to String[]?" Watch "Can I caste Object[] to String[]?" New topic
Author

Can I caste Object[] to String[]?

Zhixiong Pan
Ranch Hand

Joined: Jan 25, 2006
Posts: 239
Hi ranchers,

I am not sure can an Object[] be casted to String[]. Or should do the casting like this:


SCJP 1.4 SCJD
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14114
    
  16

Yes, you can cast an Object[] to a String[], but like with any cast, if the runtime type of the object you are casting is something else than String[], you will get a ClassCastException.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Jeroen T Wenting
Ranch Hand

Joined: Apr 21, 2006
Posts: 1847
In other words, you can try but it won't work.
Both Object[] and String[] derive from Array so there's no parent-child relationship between them.


42
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
Originally posted by Jeroen T Wenting:
In other words, you can try but it won't work.
Both Object[] and String[] derive from Array so there's no parent-child relationship between them.


Huh?



A common mistake people make with casting arrays is assuming that if an array *contains* only Strings, then it can be successfully cast to String[]. As the above example shows, that's not enough. Arrays (even empty arrays) have a specific component type, which must be String for that cast to work.

If you could successfully cast an array to String[] when it contains only Strings, explain the following code:


There is no emoticon for what I am feeling!
bob jacosin
Greenhorn

Joined: May 05, 2009
Posts: 4
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19674
    
  18

Jeroen T Wenting wrote:In other words, you can try but it won't work.
Both Object[] and String[] derive from Array so there's no parent-child relationship between them.

Actually, if a class C is a subclass of class S, then class C[] is a subclass of class S[]. So a String[] IS-A Object[], and an Integer[] IS-A Number[] IS-A Object[].


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Dawn Charangat
Ranch Hand

Joined: Apr 26, 2007
Posts: 249
Always better to do:

stringArray[i] = objectArray[i].toString();

rather than:

stringArray[i] = (String)objectArray[i]

This will make sure that there is no classcast exception, as toString() method is defined in Object class... but make sure during your object modelling and design, that toString() is aptly overridden to return the right data you require.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38456
    
  23
Bob Jacosin, welcome to the Ranch

Please use the CODE button when posting code; it preserves indentation, etc. I have edited your post so you can see the effect
Vlado Zajac
Ranch Hand

Joined: Aug 03, 2004
Posts: 245
Rob Prime wrote:
Actually, if a class C is a subclass of class S, then class C[] is a subclass of class S[]. So a String[] IS-A Object[], and an Integer[] IS-A Number[] IS-A Object[].


If C is subclass of S, then C[] is subtype of S[] but not subclass of S[]. All array types are direct subclasses of Object.

JLS - 10.8 and JLS - 4.10.3
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19674
    
  18

You are right of course.
 
jQuery in Action, 2nd edition
 
subject: Can I caste Object[] to String[]?