aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes String == with a twist... 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 "String == with a twist..." Watch "String == with a twist..." New topic
Author

String == with a twist...

Sameer V
Greenhorn

Joined: Feb 20, 2001
Posts: 4
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
Tom Pridham
Ranch Hand

Joined: Feb 06, 2001
Posts: 92
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.
natarajan meghanathan
Ranch Hand

Joined: Feb 01, 2001
Posts: 130
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
***********************************************


Sun Certified Programmer for Java 2 Platform
sona gold
Ranch Hand

Joined: Feb 14, 2001
Posts: 234
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

sona<br />SCJP
Raghvendra Sharma
Ranch Hand

Joined: Oct 09, 2000
Posts: 82
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");
****************************************************************
Vegad Arvind
Ranch Hand

Joined: Jan 10, 2001
Posts: 42
Hi
Excellent Discussion on String == from Maha Anna
<A HREF="http://www.javaranch.com/maha/Discussions/java_lang_Package/String_literals_-_JavaRanch_Big_Moose_Saloon.htm</A>" TARGET=_blank>http://www.javaranch.com/maha/Discussions/java_lang_Package/String_literals_-_JavaRanch_Big_Moose_Saloon.htm[/URL]

Hope this will help
Avi
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: String == with a twist...
 
Similar Threads
Different behaviour with string objects
Why are same stringbuffers not equal?
equality operator still confuses me...
The " == " sign is confusing....
Why are same strings not equal