• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

String Question

 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
just when i thought i knew it all:

String str="String";
if("String".toString() == str.toString())
System.out.println("Equal");
else
System.out.println("Not Equal");
prints equal......why???
if("String".replace('g','G') == "String".replace('g','G'))
System.out.println("Equal");
else
System.out.println("Not Equal");
prints not equal.....why?
if("String".replace('t','t') == "String".replace('t','t'))
System.out.println("Equal");
else
System.out.println("Not Equal");
prints equal.....why?
thanks in advance

 
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
in the first piece of code following takes place behind the scene:
String str="String"; //creates a String object.
When u use "String".toString(),no new object is being created,instead String pool is used.So both refer to the same instance of String.Hence == returns Equal.
in the second piece of code:
2 new String objects are creatd that contain the modifications that take place because of replace operation of String.Hence == returns Not Equal.
i am not sure of the third code.
 
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi mansoor
this is what i think:
1.
String str="String"; //gets put into a string pool
str.toString() //returns 'String', but 'String' is already in string pool
"String".toString() // returns 'String', but 'String' is already in string pool
so they both have reference to the same string, so '==' returns true.
2.it seems that with this, both statements
"String".replace('g','G') check the string pool and find that there is no matching string, so they both return different references. so unequal.
if u said ("String".replace('g','G')).intern() to both it would return equal as they would both be put in the string pool but the strings are the same so they would both refer to onw string so same reference.
3.with this, both statements
("String".replace('t','t') check the string pool and find a matching string so they both return the same reference, so equal
[This message has been edited by lee dalais (edited March 30, 2001).]
 
Try 100 things. 2 will work out, but you will never know in advance which 2. This tiny ad might be one:
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
    Bookmark Topic Watch Topic
  • New Topic