We can use the equals method to check if two objects are equal.
In this lesson, you're going to add an equals method to a class.
You can access this lesson by following this path and the resources and open the equals folder.
The default equals method compares the references of two objects.
Create one object of the car class.
Toyota blew.
And then set a variable, same car equal to the car.
Now, both car and same car share a reference to the same object.
Check if CA equals same car.
And it returns true because both variables store the same reference.
When you set same car equal to car, it copies the reference, and since both share the same reference,
then the equals method determines that they are indeed equal.
But that's the thing, too, variables should not share a reference to the same object.
Otherwise, you're going to risk falling into the reference trap.
So we're going to see that same car equal to a new copy of the first car object.
The two objects are exact copies of each other, but because they don't share the same reference than
the equals method determines that they're not equal.
We can customize the equals method for a specific class.
We can customize it to compare the fields of two objects, we're going to customize the equals method
for objects of the car class.
And as you can see, the equals method returns a boolean and it receives an object, what's the type
of that object?
We don't know.
It can be any object.
OK, car calls the equals method and pass the same cars in argument.
This points to the current object calling the equals method.
OBJ points to the object that was passed in.
And nothing happens inside the function, it just returns true.
That's not very useful.
Here is how to make the perfect equals method.
First, you're going to return false if the object that was passed then is null.
Then you're going to return false if the object is not an instance of the specified type.
If it happens to be an instance of the specified type, then you're going to typecast it.
Then you're going to compare every field from both objects and return the results.
In the equals method check if the object was passed in, is No.
And if that's the case, return false, there's no way the object that's calling the equals method is
going to equal null, otherwise it would have thrown a null pointer exception.
All right, now return false if the object that was passed in is not an instance of car and we can do
that using the instance of keyword, so we'll check if the object is an instance of car and reverse
it to say if the object is not of type car, then we'll return false.
Although if the object was passed in is an instance of car, then we are going to make it to this line
so we can typecast object to a car type.
And now we can access Hadfield's.
So what I'm going to do is compare every field from the current object calling the equals method, this
with every field from the car object that was passed in.
OK, time to test it inside, men, I'm going to pass a nail into the equals method.
This points to the current object and the argument we passed in is no to the equals method is going
to return false because there's no way a null is going to equal the object that's calling this method.
All right, now I'm going to create a scanner object.
And pass it into the equals method.
This points to the car object calling the method the object we passed in with scanner.
The object is not no to passes this test.
But the object is not an instance of car, so there's no way it's going to equal the car object that's
calling this method equals returns false and determines that these objects are not equal.
We'll try that out.
The object is not know, so it passes this test.
The object is of type car, so it passes that test.
The fields from both objects are equal to each other, so this condition returns true and the equals
method tells us that they are equal.
OK, now we're going to compare two car objects that don't equal each other, so I'll change this to
Honda.
All right, this object is not known.
So it passes this test.
The object is of type car, so it passes this test.
But the fields are equal to each other.
So this condition returns false and the equals method determines that these objects are not equal,
and you know what?
If you ask me, it works perfectly.
To recap, you customize the equals method for the car class.
The default equals method compares the references of two objects, which isn't very useful.
We customize the equals method to compare the fields from two car objects.