• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Can I caste Object[] to String[]?

 
Zhixiong Pan
Ranch Hand
Posts: 239
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ranchers,

I am not sure can an Object[] be casted to String[]. Or should do the casting like this:
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15480
43
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jeroen T Wenting
Ranch Hand
Posts: 1847
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
bob jacosin
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Rob Spoor
Sheriff
Pie
Posts: 20659
64
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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[].
 
Dawn Charangat
Ranch Hand
Posts: 249
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 50171
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 245
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20659
64
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are right of course.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic