aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Integer.valueOf method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Integer.valueOf method " Watch "Integer.valueOf method " New topic
Author

Integer.valueOf method

Sharmila Punde
Ranch Hand

Joined: Dec 26, 2007
Posts: 133
Hi All,
Please help to understand valueOf method of wrapper classes.

This method documentation says," This method is likely to yield significantly better space and time performance by caching frequently requested values".
So what is range of value this method can cache? And return type of this method is 'Integer' but watch the output of the following code

The output is - true
As method returns Integer, i was expecting output as false.
Look at one more excerpt

The output is - false

Why this time output is false? Why second excerpt do not unbox the returned Integer?
Regards

Regards, Sharmila.
We will have a future if we save Nature!
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9302
    
  17

Why second excerpt do not unbox the returned Integer?

Neither of the comparisons will unbox the integer. The first one returns true because the Integer objects returned by the valueOf method was from the integer pool. Also only the valueOf method which takes an int says that it returns values from the cache (or pool we can say), the valueOf method which takes String as an argument doesn't say that it will return values from the cache...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Salil Vverma
Ranch Hand

Joined: Sep 06, 2009
Posts: 255

Hey Sharmila,

As mentioned by Ankit, the valueOf function returns the object from integer pool if the value is between -128 to 127 (including these values), else create a new object. The value -128 is in this range so it will return object from constant pool that is why the first comparision returns true.

Integer valueOf(String s) never picks the value from constant pool. but each time create new object, that is why the second comparision returns false.

I hope this clears your doubt.


Regards
Salil Verma
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18829
    
  40

Sharmila Punde wrote:This method documentation says," This method is likely to yield significantly better space and time performance by caching frequently requested values".
So what is range of value this method can cache? And return type of this method is 'Integer' but watch the output of the following code


The JLS specifies the range (same range for a byte type) that must be cached -- meaning -128 to 127. It doesn't mention what should happen outside of this range. This means that it is perfectly valid to have a JVM implementation that caches a larger range.

So, to answer your question... minimum required range is -128 to 127.

Henry


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

Joined: Dec 26, 2007
Posts: 133
Dear Ankit,Salil,Henry,
Thank you very much for your help.
Now it is clear to me what was the exact reason.
Thanks again,
Wish you Happy Diwaali.
Regards
Ankur kothari
Ranch Hand

Joined: Sep 06, 2009
Posts: 531


Neha Daga
Ranch Hand

Joined: Oct 30, 2009
Posts: 504
what do you want?


SCJP 1.6 96%
Ankur kothari
Ranch Hand

Joined: Sep 06, 2009
Posts: 531
i want to clear scjp

thought of putting a nice explanation here......
Neha Daga
Ranch Hand

Joined: Oct 30, 2009
Posts: 504
....when are you appearing for the exam?
Ankur kothari
Ranch Hand

Joined: Sep 06, 2009
Posts: 531
just before the new year
Rein de Boer
Greenhorn

Joined: Jul 12, 2009
Posts: 15
Sharmila Punde wrote:Hi All,


The output is - false

Why this time output is false? Why second excerpt do not unbox the returned Integer?
Regards


To make matters complicated, on my JVM, this does print 'true'
Looking in the source the function with parameter string is implemented like this:


Latest JVM 6, windows
Rein de Boer
Greenhorn

Joined: Jul 12, 2009
Posts: 15
See also:
http://www.coderanch.com/t/474005/Programmer-Certification-SCJP/certification/what-answer#2123209


Apparently JVM dependend.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Integer.valueOf method