• Post Reply Bookmark Topic Watch Topic
  • New Topic

Can I caste Object[] to String[]?  RSS feed

 
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:
 
Java Cowboy
Sheriff
Posts: 16083
88
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.
 
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.
 
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:
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Sheriff
Posts: 21330
87
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[].
 
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.
 
Marshal
Posts: 59146
180
  • 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
 
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
Posts: 21330
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are right of course.
 
yeah, but ... what would PIE do? Especially concerning this tiny ad:
Why should you try IntelliJ IDEA ?
https://coderanch.com/wiki/696337/IntelliJ-IDEA
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!