File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Object

 
Vidya Singh
Greenhorn
Posts: 28
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 189
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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� ]
 
sachin verma
Ranch Hand
Posts: 177
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic