Win a copy of Terraform in Action this week in the Cloud forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

String == with a twist...

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Consider this piece of code...
String q = "StrinG";
String r = "String".replace('g', 'G');
String s = "StrinG";

if(r == s)
System.out.println("Equal");
else
System.out.println("Not Equal");

if (q==s) System.out.println("q does equal s");
The output is :
Not Equal
q does equal s
Why does r==s evaluate to false? I know we have two different String objects but why are two different objects created? If java maintains a pool of Strings objects, shouldn't q, r and s all be pointing to the same object in memory?
I'd appreciate comments.
Sameer
 
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because once you call the replace() method, the variable r now points to a String object instead of a string literal.
So r is pointing to a String object and s is pointing to a string literal. So r == s is false.
 
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
A new string object will be created when you call replace() method only if there is a need for it (if a new string needs to be created it is created as a separate object). For example, in this case
String q = "StrinG";
String r = "StrinG".replace('g', 'G'); //2
String s = "StrinG"; //3
if(r == s)
System.out.println("Equal");
else
System.out.println("Not Equal");
if (q==s) System.out.println("q does equal s");

first, the string "strinG", u r trying to manipulate in line 2 will be created in the string pool. then the replace() will be executed. There is no effect of that method here, since there is no 'g' to replace in the string. so the reference r will also point to the same string variable "strinG" in the pool. The in line 3, u r again pointing to the same string literal in the pool. So r == s will now return true.
Hope this helps,


------------------

***********************************************
Sun Certified Programmer for Java 2 Platform
***********************************************
 
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
but shouldn't we be using (equals) to compare strings
i have just started java
just the basics , i haven't started the string class though
so correct me if i am wrong
 
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
nice to have answer from likes of Natrajan.
But what i think is the legible result of string handling by compiler. The compiler creates an object for q ( "StrinG" ) and maintains it in the pool.
Since s asks for the "same" string "StrinG" , it gets it from the pool without needing to create object.
But in the case of r, you try to create a string object in two operations. first by using a literal and then by a method on that. By my perception of pooling and string handling of java, the compiler automatically creates an object for the literal "String" and then goes for the method replace on that object. Since an object has been created already, there is no point in asking for equality of q and r or for that matter r and s.
I think i am able to answer it.
If i am wrong at any place in my concept, please feel free to correct me.
Raghvendra Sharma
s_raghu20@yahoo.com
*******************************************************
String q = "StrinG";
String r = "String".replace('g', 'G');
String s = "StrinG";
if(r == s)
System.out.println("Equal");
else
System.out.println("Not Equal");
if (q==s) System.out.println("q does equal s");
****************************************************************
 
Ranch Hand
Posts: 42
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic