Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Autoboxing feature

 
kanaka krishna
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic