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
posted
0
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
posted
0
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
posted
0
Thanx guys ! I now understand ! I should've looked up the API !