How simple it may seem so hard it is, testing for equality. The main reason why testing for equality is so hard in Java is that there are a lot of definitions for equality between objects.
The Java language defines the following categories which are involved in equality testing:
boolean, byte, char, double,
float, int, long and short
int a = 5;
|
new keyword. The object reference equality tests whether two
object references point to the same memory location of one and the same
object.Primitive types must be tested for equality using the ==
operator. The result of the expression is true if the value
of both operands are the same. For example all following expressions evaluate to
true:
int m = 5
|
To test if two variables refer to the same object in memory the
== operator must be used. For example the following expression
evaluates to true
String s = new String("xyz");
|
The following examples evaluate to false:
String s = new String("xyz");
|
The equality of two objects which are not null must be reached
by using the equals() method.
The method equals() is a method of the class Object
and for this reason this method may be used on every object within the Java
object space.
The idea behind this method is to test two objects for equality in the most
intuitive way. Take, for example, the class Integer which is a
representation of integer numbers. In this case the method equals
is identical to the mathematical operator "=". Example:
Integer m = new Integer(#int1);
|
Another very simple example is the class String where the
equals() method compares two strings and return true if and only
if the content of both strings is identical. Some examples:
"am i equal".equals("am i equal") returns true
"first string".equals("second string") returns false
If you also take null references into account then a general
test for equality looks like the following example:
Integer m = ...;
|
If and only if you have the garantuee that the variable m
is not null you may simplify the example into the following:
Integer m = ...; // m is garantueed not to be null
|
Some people tend not to use the equals() in some cases for which
they actually should. For example I have seen the following code on regular basis:
Integer m = ...;
|
Due to the autoboxing in Java the comparisons of (m < n) and
(m > n) are valid and are syntactically equivalent to
(m.intValue() < n.intValue()) and
(m.intValue() > n.intValue()) respectively. But the code
(m == n) is NOT equivalent to
(m.intValue() == n.intValue()).
There are some specialities with equality in combination with autoboxing and
unboxing which are worth mentioning. I'll use the Integer as an
example in this section.
01 /*
|
The program above generates the following output:
3 equals 3
int a equals int b (3 equals 3)
Integer m equals Integer n (reference to boxed 3 equals reference to boxed 3 by
Java language specifications)
Integer p does not equal Integer q (new Integer(3) is unequal to new
Integer(3))
value of Integer p equals value of Integer q (3 equals 3)
reference to Integer m equals reference of boxed int a (boxed 3 equals
boxed 3)
Integer m equals boxed int a
As a closing conclusion I'll give you a few rules of thumb to prevent programming errors.
equals()
method always, the compiler will warn you if you try the equals()
method for primitive types and other places where it isn't allowed.==-operator
do not forget to use the equals() method when working with
objects.If you have any comments please send an e-mail to Patrick Holthuizen.