• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Probably you all know it already, but still..

 
maha anna
Ranch Hand
Posts: 1467
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Probably you all know it already. I just wanted to say that we should be careful when we see code like the foll.
regds
maha anna


[This message has been edited by maha anna (edited March 02, 2000).]
 
monty
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, up stumpt me ...
I ran the code ...
Why is (byteVar.toString() == byteVar.toString()) FALSE
-----
 
Tony Alicea
Desperado
Sheriff
Posts: 3226
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The first part creates two different String objects when the toString() method is called.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maha Anna's second result was the one I found surprising. I guess the thing is that while most of the time toString() creates a whole new String, it's not actually obligated to (not like the new String() constructor is). Within the String class Sun evidently decided it was easier to just return another reference to the current instance, since it already is a string. As demonstrated here:

Thanks for pointing that out, Maha. (Is that an appropriate nickname?)
 
Rolf Weasel
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jim, I think i read that this also happens with String.toUpperCase(). If the string is already Uppercase only, the original reference is returned and no new string object is created.
 
Tony Alicea
Desperado
Sheriff
Posts: 3226
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And with String.trim() also...
 
maha anna
Ranch Hand
Posts: 1467
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have tested the foll. methods of String class. For simplicity (and laziness also ) I have omitted the args for these methods. All these return the same String object ref( upon which the method is invoked )when the returned String's content is same as the original.(case sensitive)
touppercase(),tolowercase(),toString(),substring(),concat(),trim(),valueOf(),replace()
Jim, All call me Maha ,kind of pet name..
regds
Maha anna

[This message has been edited by maha anna (edited March 02, 2000).]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic