File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Not sure what is right? 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 "Not sure what is right?" Watch "Not sure what is right?" New topic
Author

Not sure what is right?

Rajesh k Jha
Ranch Hand

Joined: May 10, 2009
Posts: 72

import java.util.*;

public class Collect {
public static void main(String[] args)
{
Set<String> s=new HashSet<String>();
s.add("apple");
s.add("Apple");
s.add("1");
s.add("1.0");

a//String []ss1=new String[4];
b//ss1=s.toArray(ss1);
c//for(String str:ss1)
//System.out.println(str);
String[]ss=(String[])s.toArray();//1
for(String str:ss)//2
System.out.println(str);//3

}
}
This question is from the Whizlabs 5.0 Simulator, where it is given that when 1,2,3 is tried then it compiles and gives the output "apple,Apple,1,1.0" while i tried it generated exception, but when i tried the a,b,c line it compiled correctly and output correcly as well,
My question is why 1,2,3 line when used it generates exception while both (abc and 123 )are right?
Jason Irwin
Ranch Hand

Joined: Jun 09, 2009
Posts: 327
Please use code tags, it will make it much easier to read your code.

Well, what was the exception? That will give you big clue as to what went wrong. Clue: What is the return type of "toArray()"? What is the return type of "toArray(T[])"?


SCJP6
Nitish Bangera
Ranch Hand

Joined: Jul 15, 2009
Posts: 537

The exception you are getting will be classcastexception. Well this is because String[] and Object[] are not related types. you can check this by using

This will show that the Object[] cannot be casted to a String[].

correct form of //1,//2 and //3 is same to a,b and c.


The thing here is that all arrays of reference types are subtypes of Object[] not the reference type of any object say String array object.


[ SCJP 6.0 - 90% ] , JSP, Servlets and Learning EJB.
Try out the programs using a TextEditor. Textpad - Java 6 api
Jason Irwin
Ranch Hand

Joined: Jun 09, 2009
Posts: 327
Nitish, that make it identical to a,b, c.

This would work too:Although I think we have now given far too much information in these answers.
Nitish Bangera
Ranch Hand

Joined: Jul 15, 2009
Posts: 537

Actually i din't see that. Well i edited the post. btw Jason please see your PM.
Lucas Smith
Ranch Hand

Joined: Apr 20, 2009
Posts: 804
    
    1

Jason Irwin wrote:Please use code tags, it will make it much easier to read your code.

Well, what was the exception? That will give you big clue as to what went wrong. Clue: What is the return type of "toArray()"? What is the return type of "toArray(T[])"?

To make it easier :
Object[] toArray()
T[] toArray(T[])


SCJP6, SCWCD5, OCE:EJBD6.
BLOG: http://leakfromjavaheap.blogspot.com
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Not sure what is right?