• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
srilatha kareddy
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 ]
 
steven gerrard
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
 
Henry Wong
author
Marshal
Pie
Posts: 21427
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic