File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Associate Certification (SCJA,OCAJ 5/6) and the fly likes Autoboxing feature Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Associate Certification (SCJA,OCAJ 5/6)
Bookmark "Autoboxing feature" Watch "Autoboxing feature" New topic
Author

Autoboxing feature

kanaka krishna
Greenhorn

Joined: Oct 31, 2007
Posts: 18
Hi Cameron,
The following code snippet works fine

Vector<Double> v=new Vector<Double> ();
v.add(10.9);
double d=v.elementAt(0);

In your book(Mock Exam Questions: Pg 200) you have said this will not work.
Please correct me if Iam wrong.

Thank you
Kanaka
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4968

Indeed, the generics feature will actually cause the code to compile and run.

In the very latest printing, the question was corrected to what was originally intended:

Vector v=new Vector ();
v.add(10.9);
double d=v.elementAt(0);

Which causes the 'incompatible types' error message to be generated.

Kindest regards,

-Cameron
kanaka krishna
Greenhorn

Joined: Oct 31, 2007
Posts: 18
Model Exam Questions Answer 9-3(Pg 200)

double d=v.elementAt(0);
If we dont add the <Double> to the Vector keyword, it doesnt compile at all, so that is a must, apart from that the autoboxing converts the instance of the object to float or double, so this would be one of the correct answers.
Am I right?

In your next question's answer (9-4), the answer you have given is Double d=(Double)v.elementAt(0); which does'nt work either.
I think the correct answer should be

Float f=(Float)v.elementAt(0); and/or
float f=(Float)v.elementAt(0);
What do you say?

Thanks
Kanaka

[ November 21, 2007: Message edited by: kanaka krishna ]

[ November 21, 2007: Message edited by: kanaka krishna ]
[ November 21, 2007: Message edited by: kanaka krishna ]
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4968

I just answered the same question in an email to you.

I try not to hijack the SCJA forum to talk directly about my books. Just email me through the ranch, or through my personal email, and I'll get back to you much faster. I visit the ranch when I've got the time, but I always check my email regularly.

Kind regards!

-Cameron McKenzie
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Autoboxing feature
 
Similar Threads
problem about vector
Value in dropdown from servlet
Generic Ques
problem different from c++
The == operator