• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Shivaprasad P Kanaganahallimath
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Line 2 is evaluated as: Short story = Short.valueOf((short)5);
 
Shivaprasad P Kanaganahallimath
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Waw!! thank you so much. I didn't know that it can take short as argument.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic