Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
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
Author

Object

Vidya Singh
Greenhorn

Joined: Mar 21, 2007
Posts: 28
src:http://technoheads.blogspot.com/
3) if ("string".toUpperCase() =="STRING")
{
System.out.println("Yes");
}
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")
{
System.out.println("Yes");
}
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.

Regards
[ 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
Hint
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 object.now think about it


SCJP || SCWCD

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Object