• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Integer.valueOf method

 
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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...
 
Ranch Hand
Posts: 257
Hibernate Oracle Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Sharmila Punde
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


 
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what do you want?
 
Ankur kothari
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i want to clear scjp

thought of putting a nice explanation here......
 
Neha Daga
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
....when are you appearing for the exam?
 
Ankur kothari
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
just before the new year
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
See also:
https://coderanch.com/t/474005/Programmer-Certification-SCJP/certification/what-answer#2123209


Apparently JVM dependend.
 
reply
    Bookmark Topic Watch Topic
  • New Topic