Interesting question. The common implementation of String.toUpperCase() will actually return the original object if the object is already all uppercase, which is why (s == str) evaluates to true in your program. However, as far as I can tell, this is not a behavior guaranteed by the API, so this behavior can theoretically be different for other library implementations (someone please correct me if I'm incorrect).
The moral of the story? Use String.equals() exclusively whenever you want to test for string equality, and you won't get tripped up by these implementation-specific quirks.
toUppercase returns the original string if there is nothing to do, I found this in some Sun documentation its not well documented though, i.e. I've seen comments in the online java docs that don't make this clear at all.
"Eagles may soar but weasels don't get sucked into jet engines" SCJP 1.6, SCWCD 1.4, SCJD 1.5,SCBCD 5
Converts all of the characters in this String to upper case using the rules of the default locale, which is returned by Locale.getDefault.
If no character in this string has a different uppercase version, based on calling the toUpperCase method defined by Character, then the original string is returned.
Otherwise, this method creates a new String object representing a character sequence identical in length to the character sequence represented by this String object and with every character equal to the result of applying the method Character.toUpperCase to the corresponding character of this String object. "
In answer to the new question the rule of thumb is if the string literal is known at compile time its pooled , at run time a new string is created and you have to call intern to pool it but see the above for one exception, to start with ;-)
Kelvin Chenhao Lim
Joined: Oct 20, 2007
Originally posted by Chris Hurst: If no character in this string has a different uppercase version, based on calling the toUpperCase method defined by Character, then the original string is returned.
Interestingly, this paragraph is no longer present in the API documentation for Java 1.4 and 1.5--which suggests that this behavior is no longer guaranteed. So right now this is a implementation-specific optimization quirk rather than officially specified behavior.
the stringobject.toUpperCase()will return the original string if all charecters are in uppercase but if all charecters are'nt capital case then as we know strings are immutable so a new string object is returned with all charecters in uppercase and as all objects are stored on the heap so the returned string object too will be placed on the heap
Originally posted by Chris Hurst: For Jesper ;-) ...
Thanks Chris, but this was meant for the original poster (Akhil Maharaj). If you copy a question from a book or mock exam, we want you to tell us where you copied it from. The quote your sources page explains why this is necessary.
Joined: Sep 29, 2007
While working on the questions given in one of the mock exams , I came across this . This problem may not be exactly same as the one , I have asked here . I have added some more complications into it .
Thanks to all those for jumping on complicated String issue.