wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Probably you all know it already, but still.. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Probably you all know it already, but still.." Watch "Probably you all know it already, but still.." New topic
Author

Probably you all know it already, but still..

maha anna
Ranch Hand

Joined: Jan 31, 2000
Posts: 1467
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

Joined: Mar 01, 2000
Posts: 8
Ok, up stumpt me ...
I ran the code ...
Why is (byteVar.toString() == byteVar.toString()) FALSE
-----
Tony Alicea
Desperado
Sheriff

Joined: Jan 30, 2000
Posts: 3222
    
    5
The first part creates two different String objects when the toString() method is called.

Tony Alicea
Senior Java Web Application Developer, SCPJ2, SCWCD
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
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?)


"I'm not back." - Bill Harding, Twister
Rolf Weasel
Ranch Hand

Joined: Feb 26, 2000
Posts: 82
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.


May the Moose be with you.
Tony Alicea
Desperado
Sheriff

Joined: Jan 30, 2000
Posts: 3222
    
    5
And with String.trim() also...
maha anna
Ranch Hand

Joined: Jan 31, 2000
Posts: 1467
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).]
 
Consider Paul's rocket mass heater.
 
subject: Probably you all know it already, but still..