• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Integer.valueOf method

 
Sharmila Punde
Ranch Hand
Posts: 133
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Ankit Garg
Sheriff
Posts: 9497
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • 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...
 
Salil Vverma
Ranch Hand
Posts: 255
Hibernate Oracle Spring
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Henry Wong
author
Marshal
Pie
Posts: 20836
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Ankur kothari
Ranch Hand
Posts: 531
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


 
Neha Daga
Ranch Hand
Posts: 504
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what do you want?
 
Ankur kothari
Ranch Hand
Posts: 531
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i want to clear scjp

thought of putting a nice explanation here......
 
Neha Daga
Ranch Hand
Posts: 504
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
....when are you appearing for the exam?
 
Ankur kothari
Ranch Hand
Posts: 531
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
just before the new year
 
Rein de Boer
Greenhorn
Posts: 15
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic