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 Self Test P#259.Q#2 Please Clarify this 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 "Self Test P#259.Q#2 Please Clarify this" Watch "Self Test P#259.Q#2 Please Clarify this" New topic
Author

Self Test P#259.Q#2 Please Clarify this

Shivaprasad P Kanaganahallimath
Ranch Hand

Joined: Sep 25, 2006
Posts: 48
Hi All,
I would like thank Kathy Seirra and Bert Bates for the wonderful book .....

Could any one clear my doubt, .. the question goes like this ...

class CardBoard{
Short story=5; // How is the Boxing happening here?
CardBoard go(CardBoard cb){
cb=null;
return cb;
}
public static void main(String []args){
CardBoard c1=new CardBoard();
CardBoard c2=new CardBoard();
CardBoard c1=c1.go(c2);
c1=null;
//do stuff
}}

what's happening that 2nd line. ?

thanks in advance..

Prep for SCJP(Oct 10,2006)
Shivaprasad P.K.


Shiva Prasad P.K.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Line 2 is evaluated as: Short story = Short.valueOf((short)5);


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Shivaprasad P Kanaganahallimath
Ranch Hand

Joined: Sep 25, 2006
Posts: 48
But this is implicit typecast and it can happen from short to int. In case of explicit this (short)5 can be done rifht? and more over valueOf()takes a string as argument.

:roll:
thanks anyway for your help.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Please take the trouble to check the Java 5.0 API for Short.
valueOf

public static Short valueOf(short s)

Returns a Short instance representing the specified short value. If a new Short instance is not required, this method should generally be used in preference to the constructor Short(short), as this method is likely to yield significantly better space and time performance by caching frequently requested values.

Parameters:
s - a short value.
Returns:
a Short instance representing s.
Since:
1.5


Short.valueOf takes a short as well as a String. If you looked at the byte code generated by the compiler (as I did before answering your question) you will see that what I have written is correct.
[ October 02, 2006: Message edited by: Barry Gaunt ]
Shivaprasad P Kanaganahallimath
Ranch Hand

Joined: Sep 25, 2006
Posts: 48
Waw!! thank you so much. I didn't know that it can take short as argument.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Self Test P#259.Q#2 Please Clarify this