Tech With Tim Logo
Go back

Overloading Methods

Comparisons

We often take for granted the fact that we can compare objects using operators like ==, >, < etc. When in reality someone had to code that functionality, allowing us to use those operators. For example when we use a String object we can compare it to another using the .equals() method. This method had to be written by someone. In this tutorial I will show you how to make your objects comparable!

Example Class

For this tutorial we will use an example class called Student. We will try to create some methods that we can use to compare Students.

public class Student{
    private String name;

    public Student(String name){
        this.name = name;
    }
}

The .equals() Method

One of the most common comparison methods that most objects contain is .equals(). We use this to compare if two objects are equivalent, but how do we decide if two objects are equivalent? Well when we create our own .equals method that part is up to us. For our student example lets say two students are equivalent if they have the same name.

public class Student{
    private String name;

    public Student(String name){
        this.name = name;
    }

    public boolean equals(Student other){
        if (other.name.equals(this.name)){
            return true;
        }else{
            return false;
        }
    }
}

Now that we have created a .equals() method we can use it to compare our students.

public class Main{
    public static void main(String[] args){
        Student s1 = new Student("tim");
        Student s2 = new Student("joe");
        Student s3 = new Student("tim");

        System.out.println(s1 == s3); // prints false

        System.out.println(s1.equals(s2)); // prints false
        
        System.out.println(s1.equals(s3)); // prints true

    }
}

Notice in the above example when we compare s1 with s3 using == we get false, even though their names are the same. Well this is because what the == operator does is check if s1 and s3 are the same object, which they are not. However, when we use .equals() we run our own comparison on the objects and determine that they are indeed the same (based on our criteria).

.compareTo Method

Now that we've created our .equals method its time to define a way to order our students. We need some way to say "tim" is greater than "joe" or vice versa. To do this we start by implementing what we call the Comparable interface. Then we can write the .compareTo method. For our purposes we will order students alphabetically so we can just use the String compare to method to compare the students names.

public class Student implements Comparable<Student>{
    private String name;

    public Student(String name){
        this.name = name;
    }

    public boolean equals(Student other){
        if (other.name.equals(this.name)){
            return true;
        }else{
            return false;
        }
    }

    public int compareTo(Student other){
        return this.name.compareTo(other.name);
    }
}

Our compareTo method will return an int where:

1 means greater than

0 means equal to

-1 means less than

public class Main{
    public static void main(String[] args){
        Student s1 = new Student("tim");
        Student s2 = new Student("joe");
        Student s3 = new Student("tim");

        System.out.println(s1.compareTo(s2)); // prints 1
    }
}

.toString() Method

The .toString() method is used to be able to represent an object as a string. If you don't have a toString method and you want to print an object or information about it, it can be very difficult. We will write a .toString method for our student that will tell us the object is of type student and the students name.

public class Student implements Comparable<Student>{
    private String name;

    public Student(String name){
        this.name = name;
    }

    public boolean equals(Student other){
        if (other.name.equals(this.name)){
            return true;
        }else{
            return false;
        }
    }

    public int compareTo(Student other){
        return this.name.compareTo(other.name);
    }

    public String toString(){
        return "Student(" + this.name + ")";
    }
}
Design & Development by Ibezio Logo