Win a copy of Head First Android this week in the Android forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

String.valueOf("127") is not supposed to be cached ?

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I don't understand this behavior on my computer :



Result:
true
true
true

Integer.valueOf(int) method has a cache, but not the Integer.valueOf(String) method :



How to explain the equality between i2 and i3 references ?

Thanks for your help !
 
author
Posts: 23909
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
Interesting. It looks like the valueOf(String) method may have changed recently. Here is the source code from the build that I am using...



Henry
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am not certain if this helps. What I gather is that due to memory effeciency reasons two primitive objects holding values between -128 to 127 will return true when compared using ==.
 
Henry Wong
author
Posts: 23909
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
"kawshik java".

Please check and read the private message sent earlier again. The requirement is not optional.

Thanks,
Henry
 
Jean Dupond
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The valueOf(String) method has effectively changed.

As far as I understand, for recent 1.6 versions :



and for older 1.6 versions :


According to the Java website documentation http://download.oracle.com/javase/6/docs/api/java/lang/Integer.html#valueOf%28java.lang.String%29, the valueOf(String s) method "returns an Integer object equal to the value of: new Integer(Integer.parseInt(s))" :



Surprinsingly, the documentation does not mention the cache applied to frequently requested values.
 
Henry Wong
author
Posts: 23909
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

Jean Dupond wrote:
According to the Java website documentation http://download.oracle.com/javase/6/docs/api/java/lang/Integer.html#valueOf%28java.lang.String%29, the valueOf(String s) method "returns an Integer object equal to the value of: new Integer(Integer.parseInt(s))" :



At first I thought that the online JavaDoc was outdated -- but looking at the source, it looks like the code was updated but the comments (used by javadoc) wasn't.... meaning documentation bug.

I wondered why the Sun engineer didn't take five minutes to fix the comments as he/she changed the code -- a bit lazy IMO.

Henry
 
Ranch Hand
Posts: 394
Eclipse IDE Oracle Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Guys, does this new info. affect those preparing for the OCPJP?

Regards

Ikpefua.
reply
    Bookmark Topic Watch Topic
  • New Topic