Thursday, 20th October 2011
Follow WikiJava on twitter now. @Wikijava

Making confrontable Classes

From WikiJava

Jump to: navigation, search
The author suggests:

buy this book

This article shows how to create a class whose instances are comparable. This means that when you have two objects from this class they can be compared and a boolean value true or false can be obtained whether the two objects are equal or not.

Contents

Explanation

When you write in Java a statement like this:

if (var1 == var2) {
    //do something
}

The == sign has a very precise meaning:

  • If var1 and var2 are primitive types (int, float, char .... ) then return a true if the two values are the same
  • If var1 and var2 are Objects, then return true if they are the same object.

There's a very simple reason to this, and it depends on how Java refers to objects internally.

see now the following example:

String var1 = new String ("a"); 
String var2 = new String ("a"); 
 
if (var1 == var2) {
    //this is never executed.
}

Why the code inside the If statement is never executed? because var1 and var2 are different objects, it doesn't matter that they contain exactly the same String.

In general confrontable Objects will always implement the equals() method. Which returns true if the two objects are equal, basing on certain criteria which is always dependent on the specific class.

The example above can be corrected now in:

String var1 = new String ("a"); 
String var2 = new String ("a"); 
 
if (var1.equals(var2)) {
    //this is always executed.
}

Note that now there's a call to the method equals, rather than ==.

NOTE: It is very simple to get confused and use == instead of equals() inside an If statement. This leads very often to problems during execution time. The Javac doesn't signal any error or warning either, as both syntaxes are perfectly valid. So any time you use == always remember to double check if that's really what you want to do. Or you are incurring in this common mistake. 

How to implement the equals method in your classes and make them comparable? That's very simple. Let's see it in the example below, taken from the class Job:

	public boolean equals(Job job) {
 
		if (!this.contactEmail.equals(job.getContactEmail())) {
			return false;
		}
		if (!this.description.equals(job.getDescription())) {
			return false;
		}
		if (!this.title.equals(job.getTitle())) {
			return false;
		}
 
		return true;
	}

As you can see this is a normal method, with signature:

public boolean equals(Job job)

Where the Type of the parameter must be the same as the class (the parameter must be Job in the class Job, and it is String in the class String).

The method just checks the various values (or it performs any algorithm) and returns true if the two objects are equal.

That's it. easy no?


Comments from the users

To be notified via mail on the updates of this discussion you can login and click on watch at the top of the page


Comments on wikijava are disabled now, cause excessive spam.