aspose file tools*
The moose likes Beginning Java and the fly likes Why are same strings not equal Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Why are same strings not equal" Watch "Why are same strings not equal" New topic
Author

Why are same strings not equal

Chandra Bairi
Ranch Hand

Joined: Sep 12, 2003
Posts: 152
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


Thanks,
Shekar
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

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.


[Jess in Action][AskingGoodQuestions]
Vinod Chandana
Ranch Hand

Joined: Aug 26, 2003
Posts: 59
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

Joined: Sep 12, 2003
Posts: 152
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

Joined: Jul 08, 2003
Posts: 24187
    
  34

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
 
subject: Why are same strings not equal