Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why are same strings not equal

 
Chandra Bairi
Ranch Hand
Posts: 152
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class ADirtyOne
{
public static void main(String[] a) {

if("String".replace('g','G') == "String".replace('g','G'))
System.out.println("Equal");
else
System.out.println("Not Equal");
}
}
the o/p to the above is NotEqual why is this not equal
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The "==" operator compares two physical objects in memory to see if they occupy the same location in RAM -- i.e., to see if they are physically the same object. That won't be the case in your example; the two strings are being dynamically created by calling "replace", and each one will be a distinct object created just for that purpose. Their contents will be identical, but they'll be physically different.
The "equals" method, which every Java object has, is meant to compare two objects for equivalence. If you replaced the "==" line in your code with

you'd find that you'd get the results you expect. Two separate String objects are equivalent if they're the same length and include all the same characters in the same order.
So don't use "==" for comparing Strings; a wise man once compared this to the act of running with scissors.
 
Vinod Chandana
Ranch Hand
Posts: 59
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This should clear most of ur doubts. Remember that when u r reassigning a string variable, new memory is created to store the changes.
Cheers,
Vinod.

Originally posted by Chandra Bairi:
public class ADirtyOne
{
public static void main(String[] a) {

if("String".replace('g','G') == "String".replace('g','G'))
System.out.println("Equal");
else
System.out.println("Not Equal");
}
}
the o/p to the above is NotEqual why is this not equal
 
Chandra Bairi
Ranch Hand
Posts: 152
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If what you have said is correct that the two strings are different in physical locations then why does this program give the output equal. how is this different from the above prg and how can we be sure that the strings are pointing to same object or different other that executing the program.
public class ADirtyOne05
{
public static void main(String[] a) {
if("String".replace('g','g') == "String".replace('g','g'))
System.out.println("Equal");
else
System.out.println("Not Equal");
}
}
"Equal"
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To understand this, you need to know two things:
First, String literals are shard in the JVM, Every instance of "String" represents the same physical object; so "String" == "String" is true, while "String" == new ("String") is false.
Second, the guys who wrote the Java APIs are smart enough that replace(x, x) just returns the original string, unaltered.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic