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..
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?
Never ascribe to malice that which can be adequately explained by stupidity.
Avi Sridhar
Ranch Hand
Joined: May 31, 2007
Posts: 42
posted
0
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.
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.
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
posted
0
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
posted
0
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.