aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Set.toArray Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Set.toArray" Watch "Set.toArray" New topic
Author

Set.toArray

Harikrishna Gorrepati
Ranch Hand

Joined: Sep 23, 2010
Posts: 422
Hi, This is from K&B. What is the meaning of the 2 statements given in the comments.


OCPJP 6.0-81% | Preparing for OCWCD
http://www.certpal.com/blogs/cert-articles | http://sites.google.com/site/mostlyjava/scwcd |
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4490
    
    8

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.
Imad Aydarooos
Ranch Hand

Joined: Nov 02, 2010
Posts: 87
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


love demgracy, knowledge demogracy, open source and Java - OCPJP 76%
Imad Aydarooos
Ranch Hand

Joined: Nov 02, 2010
Posts: 87
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

Joined: Apr 06, 2010
Posts: 4490
    
    8

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

Joined: Nov 02, 2010
Posts: 87

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

Joined: Apr 06, 2010
Posts: 4490
    
    8

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

Joined: Nov 02, 2010
Posts: 87
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
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9321
    
  17

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...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Imad Aydarooos
Ranch Hand

Joined: Nov 02, 2010
Posts: 87
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

Joined: Aug 03, 2008
Posts: 9321
    
  17

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

Joined: Nov 02, 2010
Posts: 87
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

Joined: Apr 06, 2010
Posts: 4490
    
    8

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

Joined: Nov 02, 2010
Posts: 87
and the strange thing I noticed after posting the last question is the code in BOLD below

Imad Aydarooos
Ranch Hand

Joined: Nov 02, 2010
Posts: 87
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

Joined: Aug 03, 2008
Posts: 9321
    
  17

Primitive arrays are not subtypes of Object[], they directly inherit from Object class...
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Set.toArray