This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubts in Strings

 
jammy ponkia
Greenhorn
Posts: 15
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

Can anyone explain why the below code prints "Not Equal"?

if("String".replace('g','G') == "String".replace('g','G'))
System.out.println("Equal");
else
System.out.println("Not Equal");

Thanks,
Jaimesh.
 
Sandeep Bhandari
Ranch Hand
Posts: 201
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The String objects are immutable and every modifier method of String class returns a new object without modifying the original object.

Don't tell me you don't know that == operator checks if the references point to same object.
 
jammy ponkia
Greenhorn
Posts: 15
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sandeep,

I know that == checks whether the references are same. I have one more doubt.

why doesnt the below code print "Not Equal".

if( "STRING".toUpperCase() == "STRING".toUpperCase())
System.out.println("Equal");
else

System.out.println("Not Equal");

Thanks,
Jaimesh.
 
Marco Ehrentreich
best scout
Bartender
Posts: 1294
IntelliJ IDE Java Scala
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jammy,

this is because toUpperCase() is clever enough to realize that there's nothing to do an lets the strings untouched. Make "STRiNG" out of "STRING" for example and the output will be "Not Equal".

Marco
 
Mamadou Touré
Ranch Hand
Posts: 189
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Jammy,

As your both "STRING" are in capital letters, so the toUpperCase method will not create a new object, so it's like your comparing "STRING" to "STRING". As sson as the toUppercase makes a changes (for instance try "StRING".toUpperCase ), you'll see that your class will prints not equals.
Hope this will help

Regards
[ April 04, 2008: Message edited by: Mamadou Tour� ]
 
Sandeep Bhandari
Ranch Hand
Posts: 201
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And please note that same object is returned by String modifier methods, if their is nothing to modify.
The same happens with trim() method as in toUpperCase() as shown by you.
 
Martin Jedrzejewski
Greenhorn
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

You actually can look into String, toUpperCase() and replace() methods source code. If you have IDE like NetBeans installed you can even step into those methods with debugger and see whats going on inside.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic