Monday, October 19, 2009

Section 4.6. Comparing Reference Types







4.6. Comparing Reference Types

4.6.1. Using the Equality Operators ==

The != and == equality operators:!= and

  • Are used to compare the memory locations of two objects. If the memory addresses of the objects being compared are the same, the objects are considered equal.

  • Are not used to compare the contents of the two objects.

In the following example, guest1 and guest2 have the same memory address, so the statement "They are equal" will be output.

	Guest guest1 = new Guest("name");
Guest guest2 = guest1;
if (guest1 == guest2)
System.out.println("They are equal")


In the following example, the memory addresses are not equal, so the statement "They are not equal" will be output.

	Guest guest3 = new Guest("name");
Guest guest4 = new Guest("name");
if (guest3 == guest4)
System.out.println("They are equal.")
else
System.out.println("They are not equal")


4.6.1.1. Using the equals( ) method

To compare the contents of two class objects, the equals( )method from class Object can be used or overridden.

By default, the equals( ) method uses only the == operator for comparisons. This method has to be overridden to really be useful.


For example, if you want to compare values contained in two instances of the same class, you should use a programmerdefined equals( ) method.

4.6.1.2. Comparing strings

There are two ways to check whether strings are equal in Java, but the definition of "equal" for each of them is different. Typically, if the goal is to compare character sequences contained in two strings, the equals( ) method should be used.

  • The equals( ) method compares two strings, character by character, to determine equality. This is not the default implementation of the equals( ) method provided by the class Object. This is the overridden implementation provided by class String.

  • The == operator checks to see whether two object references refer to the same instance of an object.

Below is a program that shows how strings are evaluated using the equals( ) method and the == operator. For more information on how strings are evaluated, see the "String Literals" section in Chapter 2.

	class MyComparisons {

// Add string to pool
String first = "chairs";
// Use string from pool
String second = "chairs";
// Create a new string
String third = new String ("chairs");

void myMethod( ) {

// Contrary to popular belief, this evaluates
// to true. Try it!
if (first == second) {
System.out.println("first == second");
}

// This evaluates to true
if (first.equals(second)) {
System.out.println("first equals second");
}
// This evaluates to false
if (first == third) {
System.out.println("first == third");
}
// This evaluates to true
if (first.equals(third)) {
System.out.println("first equals third");
}
} // End myMethod( )
} //end class



Strings are immutable.


4.6.1.3. Enumerations

enum values can be compared using == or the equals( )method, as they return the same result. The == operator is used more frequently to compare enumeration types.








No comments:

Post a Comment