Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Java Data Structures - Retired Organizing Data Interfaces

if (equals(other)) test

When you create the if gate inside the compareTo method, what are we comparing other to? What are we testing 'other' to in this test, and how does that work?

1 Answer

Craig Dennis
STAFF
Craig Dennis
Treehouse Teacher

equals is an inherited method from Object. It is comparing the current instance to the passed in Object.

You could also read that like this if it helps:

if (this.equals(other))  {
/// ...
}

Hope that helps!

OK! So that could also be written as if (obj.equals(other)) {}, correct?

Craig Dennis
Craig Dennis
Treehouse Teacher

Not inside that method, because in that method obj is just the Object "version" of the parameter passed in, we then cast it to the BlogPost named other. It really is calling the inherited method that belongs to the instance.

Outside of that method, if you had an instance to any object you could call equals on it....just like we did with Strings.

"Hello".equals("Hello")