• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Set.toArray

 
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, This is from K&B. What is the meaning of the 2 statements given in the comments.
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
new String[0] creates a String array of size zero. As to why you'd want to do that, it's related to your second question.

You can't convert an Object array into a String array just because it contains only Strings. You have to have created a String array in the first place for the cast to work.

If you look at the Javadocs for the toArray method, you'll see there are two versions. The one that takes an argument gives you a way of specifying exactly what type of array is returned. Pre-generics, that meant you could cast it without the exception. Post-generics it's even better, as you don't need to cast it.
 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Matthew Brown wrote:
You can't convert an Object array into a String array just because it contains only Strings. You have to have created a String array in the first place for the cast to work.



Even if he has already created the String[] before he cast, this will not work. WHY?? because String IS A Object, so you can cast an Object to String, but String[] IS NOT A Object[], another words, String extends Object, but String[] and Object[] are different types, in fact they both extend Object (as any class in Java does):
You can say
String s = (String) Object;
String[] sArray = (String[]) Object;
but you CAN'T say String[] sArray = (String[]) objectArray
 
Imad Aydarooos
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
for this code regarding the 0 : String[] ss = s.toArray(new String[0]);
the ArrayList JavaDoc say:
"If the list fits in the specified array, it is returned therein. Otherwise, a new array is allocated with the runtime type of the specified array and the size of this list. "
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Imad Aydarooos wrote:Even if he has already created the String[] before he cast, this will not work. WHY?? because String IS A Object, so you can cast an Object to String, but String[] IS NOT A Object[]



Actually, it is. Check out the Java Language Specification. Or just try it out.
 
Imad Aydarooos
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I tried this :



and I got this:
C:\myprogs>java Question24_toArray_Ask
Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object; can
not be cast to [Ljava.lang.String;
at Question24_toArray_Ask.main(Question24_toArray_Ask.java:19)
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Imad Aydarooos wrote:
and I got this:
C:\myprogs>java Question24_toArray_Ask
Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object; can
not be cast to [Ljava.lang.String;
at Question24_toArray_Ask.main(Question24_toArray_Ask.java:19)



That's because s.toArray() doesn't return a String[], it's just a plain Object[]. That's not the same thing as saying a String[] IS NOT A Object[].

In fact, if String[] and Object[] were unrelated, you wouldn't be getting an exception. You'd get a compiler error, as the compiler would know the cast could not possibly work.

Try this:


 
Imad Aydarooos
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Matthew Brown You pointed out great points and make me rethink this issue and search the internet to resolve the conflicts in my understanding ..OK, I have these points:
1- There is sure a relation between AnyObject[] and Object[];
2- at runtime the JVM checks to see if the contents of the Object array are the same type as the target array, if they are not it throws java.lang.ClassCastException.

This is strange behavior, because I expect if a SUBClass IS A SUPERClass then we can say
SUBCLAS subClass = (SUBCLASS) superClassObject;
and I expect the Object[] elements which are Object type to be automaticly casted to String, but this dosn't happen !!!

again we can't do the above for the Object Arrays and its very strange
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't understand Imad what you are trying to say. The toArray method of List returns an Object[] that's why the cast to String[] fails. Its as simple as that.

expect the Object[] elements which are Object type to be automaticly casted to String, but this dosn't happen


The JVM will not check the elements in an array. What the JVM sees is that there is an Object[] (which may contain any elements, the JVM doesn't care) and when you try to cast it to String[], you get an exception...
 
Imad Aydarooos
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Ankit

the question is : is anyObject[] is a subtype of Object[]?
if the answer is YES, why the cast throws a cast exception at runtime even if the elements of Object[] is anyObject type ?
 
Ankit Garg
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The toArray method returns Object[] then how can you cast it to String[]?? Its like doing this
Even though objs contains String objects, the array itself is of type Object[]. So casting it to String[] is not allowed. You can try an instanceof comparison too
 
Imad Aydarooos
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Then how come this code produces true?

if the runtime object (the String[]) is assignable to the compile time object (the Object[]) then the compile time object is castable to the runtime object.
or I'm wrong ???!!!
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As Ankit suggests, what's in the array isn't what is important. It's the array itself.

Compare the following:
 
Imad Aydarooos
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
and the strange thing I noticed after posting the last question is the code in BOLD below

 
Imad Aydarooos
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Ankit Garg and thanks Matthew Brown , I got it now

1- AnyObject[] is a subtype of Object[];
2- if the runtime object is AnyObject[] and the compile time refrence is Object[], then the Object[] refrence is castable to AnyObject[] refrence .
3- this is not about the content of the array type, its about the array type
[Note: this works only for non primitive type arrays, it works only for object type arrays]
and thanks again
 
Ankit Garg
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Primitive arrays are not subtypes of Object[], they directly inherit from Object class...
 
reply
    Bookmark Topic Watch Topic
  • New Topic