File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Integer object - intValue() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Integer object - intValue()" Watch "Integer object - intValue()" New topic
Author

Integer object - intValue()

Avi Sridhar
Ranch Hand

Joined: May 31, 2007
Posts: 42
Hi All,

I have a method that takes an Integer Object, can i pass intValue() to that method instead of new Integer()- it should work the same way

From what i understand, if Integer is passed then the memory location is passed, whatever value there is in the memory location will be used as the argument (actual value at run time) - 2 steps used

But if intValue() is passed then the actual value is going to be used at run time - 1 step used..

Please let me know

thank you
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11150
    
  16

I think it would work...

intValue() returns an "int". If your method requires an Integer, then i think the int will be autoboxed to an Integer, at which point you're now back to where you were, after adding a few steps.

if you want to see if it works, why not just try it?

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Avi Sridhar
Ranch Hand

Joined: May 31, 2007
Posts: 42
fred rosenberger wrote:I think it would work...

intValue() returns an "int". If your method requires an Integer, then i think the int will be autoboxed to an Integer, at which point you're now back to where you were, after adding a few steps.

if you want to see if it works, why not just try it?


Thank you for the response. The method does not complain if i use .intValue() - it seems to be working OK. I am checking to see whether my understanding of memory locations is almost correct.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18498
    
  40

Avi Sridhar wrote:
Thank you for the response. The method does not complain if i use .intValue() - it seems to be working OK. I am checking to see whether my understanding of memory locations is almost correct.


This has nothing to do with "memory locations" or whether an int can replace an Integer, etc. Basically, the compiler sees that the method requires an Integer object, and you passed it an int primitive... and it generates the extra code needed to convert the primative int to an Integer object.

So... if you are using the intValue() method, it means that you are likely extracting the primative int from your Integer object, and the compiler then generates extra code to convert it back to an Integer for you.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Avi Sridhar
Ranch Hand

Joined: May 31, 2007
Posts: 42
Henry Wong wrote:
Avi Sridhar wrote:
Thank you for the response. The method does not complain if i use .intValue() - it seems to be working OK. I am checking to see whether my understanding of memory locations is almost correct.


This has nothing to do with "memory locations" or whether an int can replace an Integer, etc. Basically, the compiler sees that the method requires an Integer object, and you passed it an int primitive... and it generates the extra code needed to convert the primative int to an Integer object.

So... if you are using the intValue() method, it means that you are likely extracting the primative int from your Integer object, and the compiler then generates extra code to convert it back to an Integer for you.

Henry


Hi Henry, Thank you for the response. your point is well taken.
Jim Hoglund
Ranch Hand

Joined: Jan 09, 2008
Posts: 525
Avi Sridhar wrote:I am checking to see whether my understanding of memory locations is almost correct.

Learning Java I have found that thinking in terms of memory locations (or pointers)
is not very helpful. It can even be very misleading at times. Just a word of caution.

Jim ... ...


BEE MBA PMP SCJP-6
Avi Sridhar
Ranch Hand

Joined: May 31, 2007
Posts: 42
Jim Hoglund wrote:
Avi Sridhar wrote:I am checking to see whether my understanding of memory locations is almost correct.

Learning Java I have found that thinking in terms of memory locations (or pointers)
is not very helpful. It can even be very misleading at times. Just a word of caution.

Jim ... ...


thank you for your advice
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Integer object - intValue()
 
Similar Threads
JMX gauge monitor
Passed By Reference?
CAST question
how are the objects passed by value or by reference
JNI accessing Integer Object