Help coderanch get a
new server
by contributing to the fundraiser
  • 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Wrappers

 
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Smitha,

Although they for sure represent the same value, they are different objects. Each time you call toString, a new String object is created. The Byte class has its toString method overriden to convert its byte value to a String. As String objects are immutable, each time you call toString, a new String has to be created and this is why you get the "False"
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My guess is....

"==" compares if two references point to the same object or not (does not compare the state of the object or any kind if equality related to the objects).

b1.toString method (in both sides) returns two different instances of string.

That is why the "==" operator returns "false"
 
Smitha Ballikar
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks!!
 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try passing a value of "10" instead of "127" to the constructor and see the result (surprisingly, you get true).

This is due to the weird code, that exists in Integer.toString(int i) method. I'm hoping someone can explain the logic why Sun developed such an anamoly for this method.
 
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wow, that is bizarre. I never knew that. Apparently any call to Integer.toString(int) with a value between -3 and 10 inclusive will return true because it uses the same string constant. Did they somehow determine that these values were so common that having them all point to the same string representation in memory was more efficient?

EDIT:

Looking further at the JDK, it must have something to do with this "invariant divison by multiplication" algorithm they use to make the toString conversion quicker. But the algorithm is a little obscure for my small brain.
[ August 24, 2005: Message edited by: Ryan Kade ]
 
My previous laptop never exploded like that. Read this tiny ad while I sweep up the shards.
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic