• Post Reply Bookmark Topic Watch Topic
  • New Topic

why primitive variable can accept object ?  RSS feed

 
Alvin chew
Ranch Hand
Posts: 834
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi, good day , i wondering why following code is valid ?



showform is an integer which create with "new Integer(2)", so it is "2"

my understanding is request.getAttribute() will return object , it pretty well we can have cast with "Integer" , because "Integer" is class ...so it should be allowed

but my question is after the casting ..the object will become "Integer" Object, but then why we can simply just put it into primitive type "int" ?

thank you for answering
 
Martin Mathis
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What version of the JDK are you using? I know one of the new 1.5 features is auto boxing/unboxing which should make what you're code is doing allowable.
[ December 24, 2004: Message edited by: Martin Mathis ]
 
Alvin chew
Ranch Hand
Posts: 834
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oopss...i recently change to 1.5 ....so let say for 1.4 is not allow right ?

thanks, Martin Mathis for answering
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!