Win a copy of Succeeding with AI this week in the Artificial Intelligence and Machine Learning 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
  • Junilu Lacar
Sheriffs:
  • Tim Cooke
  • Jeanne Boyarsky
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • salvin francis
  • fred rosenberger
  • Frits Walraven

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

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
i just compiled it its coming false in both the cases
 
author
Posts: 23874
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
thanks for reply.
 
The happiness of your life depends upon the quality of your thoughts -Marcus Aurelius ... think about this tiny ad:
Two software engineers solve most of the world's problems in one K&R sized book
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic