aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Doubts in Strings Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Doubts in Strings " Watch "Doubts in Strings " New topic
Author

Doubts in Strings

jammy ponkia
Greenhorn

Joined: Mar 23, 2008
Posts: 15
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

Joined: Apr 16, 2004
Posts: 201
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.


SCJP 96% | SCWCD 90%| SCJP mock exams | My SCJP blog
jammy ponkia
Greenhorn

Joined: Mar 23, 2008
Posts: 15
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

Joined: Mar 07, 2007
Posts: 1278

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

Joined: Dec 27, 2007
Posts: 189
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� ]

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
Sandeep Bhandari
Ranch Hand

Joined: Apr 16, 2004
Posts: 201
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

Joined: May 14, 2007
Posts: 11
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
 
subject: Doubts in Strings
 
Similar Threads
Strings
String Methods
Doubt pertaining to '==' operator
Why are same strings not equal
String Equality