Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Declaring s Short object

 
Jason Attin
Ranch Hand
Posts: 232
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Guys,
a question Glenn, Mitchell. OCAJP Oracle Certified Associate Java SE 8 Programmer Practice Exams (Kindle Location 11085). Enthuware. Kindle Edition. prompted me to try this code which doesn't compile. I thought it would compile of course :-). So, apparently to get it to compile I have to do which I find a bit odd because that then prompts another question: how do I create a Short object normally? Any example please?
thanks

 
Henry Wong
author
Marshal
Pie
Posts: 22113
88
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First, your expression is pretty convoluted. You are taking a primitive int, which you are using to instantiate a Short object, and then, using autoboxing to unbox it to a primitive short. Isn't it much easier to just declare and assign the short, like so?



... but to answer your question ... well, I guess you can create the short instance the way that you did. That works fine. Or alternatively, you can use autoboxing to box the Short instance like so...



And finally, if you want to keep your expression convoluted, you can use autoboxing to box, and then use autoboxing again to unbox, like so...



Henry
 
Ganesh Patekar
Bartender
Posts: 696
23
Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Oracle Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This doesn't compile because Short has only two types of constructor which accepts either short Or String as parameter ---> Short. You might know 1 is an int literal so need to cast to short explicitly.
 
Jason Attin
Ranch Hand
Posts: 232
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks guys, well yes I wouldn't create a short like that myself, but that was in the mock exam questions.
You might know 1 is an int literal so need to cast to short explicitly.

OK cool, thanks
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic