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 trialNicolas Hampton
44,638 Pointsif (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
Treehouse Teacherequals
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!
Nicolas Hampton
44,638 PointsNicolas Hampton
44,638 PointsOK! So that could also be written as if (obj.equals(other)) {}, correct?
Craig Dennis
Treehouse TeacherCraig Dennis
Treehouse TeacherNot inside that method, because in that method
obj
is just theObject
"version" of the parameter passed in, we then cast it to theBlogPost
namedother
. 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")