• 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
  • Tim Cooke
  • paul wheaton
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

byteRef.toString() == byteRef.toString() ?

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Byte b1 = new Byte("9");
if(b1.toString()==b1.toString())
System.out.println("true = "+(b1.hashCode()));
else
System.out.println("false = "+(b1.hashCode()));

o/p is true 9

Byte b1 = new Byte("11");
if(b1.toString()==b1.toString())
System.out.println("true = "+(b1.hashCode()));
else
System.out.println("false = "+(b1.hashCode()));

o/p is false 11


Please explain this
[ February 17, 2006: Message edited by: s reddy ]
 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i just compiled it its coming false in both the cases
 
author
Posts: 23959
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
It depends on the JVM. Up to JDK 1.4, values from "-3" to "10" was cached -- meaning it will return the same string reference for same values, in this range. After JDK 1.4, it will return a new string everytime. (BTW, not exactly sure when it switched over)

Regardless, this is more reason to use equals() to compare strings.

Henry
 
srilatha kareddy
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks for reply.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic