aspose file tools
The moose likes Java in General and the fly likes why primitive variable can accept object ? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "why primitive variable can accept object ?" Watch "why primitive variable can accept object ?" New topic
Author

why primitive variable can accept object ?

Alvin chew
Ranch Hand

Joined: Jan 08, 2004
Posts: 834
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

Joined: Dec 20, 2004
Posts: 45
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

Joined: Jan 08, 2004
Posts: 834
oopss...i recently change to 1.5 ....so let say for 1.4 is not allow right ?

thanks, Martin Mathis for answering
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: why primitive variable can accept object ?
 
Similar Threads
Exam Lab Test 2 Question
Why a primitive variable can point to wrapper object?
Integer Class
equals
Wrapper Class Object Doubt