Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Integer object - intValue()

 
Avi Sridhar
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12086
29
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Avi Sridhar
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20894
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Avi Sridhar
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 525
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ... ...
 
Avi Sridhar
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic