File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes toUpperCase() Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Professional Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "toUpperCase()" Watch "toUpperCase()" New topic
Author

toUpperCase()

Trinity
Greenhorn

Joined: Aug 17, 2000
Posts: 15
Hi,
Consider the following code:
String s = "HELLO";
System.out.println(s == s.toUpperCase());
I thought toUpperCase() will return a NEW object and so the output will be false. But the output is true !!
Can someone please explain !!
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
hi,
toUppercase(), toLowerCase() will return the same object if the result not change.
Mick
Originally posted by Trinity:
Hi,
Consider the following code:
String s = "HELLO";
System.out.println(s == s.toUpperCase());
I thought toUpperCase() will return a NEW object and so the output will be false. But the output is true !!
Can someone please explain !!

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Hi trinity.Well you see,the methods of String class which look like modifying the string object(eg. toUpperCase(),trim() etc.) will do so ONLY IF CERTAIN CONDITIONS ARE MET.For example , toUppeCase() will return a new string object only if there is actually a lowercase char in the string.In your case there is not,so A REFERENCE TO THE ORIGINAL STRING OBJECT is returned.

------------------
Come on in !! Drinks are on the house in the Big Moose Saloon !!
Trinity
Greenhorn

Joined: Aug 17, 2000
Posts: 15
Thanx guys ! I now understand !
I should've looked up the API !
 
 
subject: toUpperCase()
 
Threads others viewed
Question Regarding Strings
question on String method
Jaworski Book Questions
String methods
WA #1.....word association
developer file tools