• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Doubts in Strings

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 201
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
best scout
Posts: 1294
Scala IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 189
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Today's lesson is that you can't wear a jetpack AND a cape. I should have read this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic