Meaningless Drivel is fun!
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Object 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 "Object" Watch "Object" New topic


Vidya Singh

Joined: Mar 21, 2007
Posts: 28
3) if ("string".toUpperCase() =="STRING")
else System.out.println("No");

Ans -> No // if we use .equals() here , then we get �yes� . This is because , �string� and �STRING� are two separate object references pointing to two separate objects. == is used to compare whether two object references point to same object or not. .equals() compares the objects for their contents.
The explaination seems not correct as if i change the code to
if ("STRING".toUpperCase() =="STRING")
else System.out.println("No");
then the answer is Yes. So, doesn't STRING".toUpperCase() as seperate object than STRING
Mamadou Touré
Ranch Hand

Joined: Dec 27, 2007
Posts: 189
Hi vidya,

if ("STRING".toUpperCase() =="STRING")
since the "STRING" in "STRING".tuUpperCase() is already all in uppercase character, so the methode toUperCase is smart enough to not make any change on it, so no new object is created, then the == gives true.

[ May 09, 2008: Message edited by: Mamadou Tour� ]

SCJP 5 (76%)
SCWCD 5 (86%)
SCBCD 5(70%)
"The greatest glory in living lies not in never falling, but in raising every time we fall.".. Nelson Mandela
sachin verma
Ranch Hand

Joined: Mar 25, 2008
Posts: 177
when you say
"string".toUpperCase() that means it is creating an object of String.That is why you are able to call a method toUpperString() on it.
and when you say
"STRING" it will create another think about it


I agree. Here's the link:
subject: Object
It's not a secret anymore!