« Order of Anchor Selectors in CSS | Main | Apple Releases Very Cool New iPods »
April 28, 2003
How Equals Works in Java
I saw some confusion about this matter on a list this morning, so I thought I'd clarify things here:
The object "Object" which all other Java objects extend has an equals method which allows you to check to see if two objects are equal, which means you call equals on any object you want. Objet's implementation of equals does the following (from the documentation):
The equals method for class Object implements the most discriminating possible equivalence relation on objects; that is, for any reference values xand y, this method returns true if and only if x and y refer to the same object ( x==y has the value true ).
When you compare two instances using ==, you are actually comparing their memory addresses to see if they are references to the same object. This is usually not very useful in the normal course of programming. Whether an object is equal to another object is pretty context-specific, meaning that it depends on the types of objects that you are comparing. Strings are a pretty straightforward case. If one string equals "abc" and another string equals "def", then if you check to see if they are equal by calling the equals method, it should return false. But if one string equals "abc" and the other string equals "abc", you don't want to compare the memory addresses of the two instances (since they will be different), but rather you want to compare the sequence of characters. Therefore, the String object override's Objet's equals method with it own implementation which simply compares the sequence of characters. Here's some code to illustrate the point:
This is true:
String a = "Christian Cantrell";
String b = "Christian Cantrell";
if (a.equals(b)) {
// This code will be executed.
}
But this is false:
if (a == b) {
// This will evaluate to false.
// Different memory addresses.
// Do this only to check to see if two
// variables point to the same object instance.
}
This is true:
if (a == a) {
// True because you are comparing the same instance.
}
And finally, this is also true:
b = a;
if (a == b) {
// True because you are comparing the same instance.
}
Many other objects override the equals method so that it makes sense contextually. I'll post more information tomorrow.
Posted by cantrell at April 28, 2003 01:05 PM | References
Comments
er sorry there is a mistake here.
You say that considering:
String a = "Christian Cantrell";
String b = "Christian Cantrell";
if (a == b) //do stuff
"//do stuff'' won't be executed. This is incorrect, it will. In Java two strings with the same value are referenced to the SAME object to save space in the internal String table. thus both a.equals (b) and a==b will be true.
You can force the JVM to allocate new memory for a string instance with:
String a = "Christian";
String b = new String ("Christian");
then a == b will be false because the JVM has created a new object, but a.equals(b) will be true because the values are the same.
cheers
john
Posted by: john summers at October 3, 2003 07:54 AM
pwned
Posted by: twism at July 27, 2005 02:02 PM
hello boss you have confused == and .equals
It will tell you in a clear way
when you create a string object for ex
String a="Suresh"; //you see here it is a canonical form a string object is created in String Pool
If you do not know about String Pool .let me explain it
a String pool is different from memory where you keep your objects
see when you use canoical form a string object is
created in String pool
BUT WHEN YOU USE String a=new String("Suresh");
a String object is created out of the String pool
so when you compare you have false returned
Posted by: Suresh at August 14, 2005 02:50 AM