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 trialLiron Tal
4,825 PointsEquals(What?)
Hi all
Craig is changing the CompareTo code in "Meet The JCF" video.
He does this: " public int compareTo(Treet other) { //Treet other = (Treet) obj; // downcasting obj which is the other Treet if(equals(other)) { // first of all check if objects are the same, and return 0 return 0; }"
Therefore I do not understand what happens now with equals(other)? It compares between...? I debugged the code and it seems that it automatically compares "this" to "other" why is that? And why not using this.equals(other)?
Please assist.
Thanks! Liron
1 Answer
jcorum
71,830 PointsIn this case this is optional as there's no ambiguity in if(equals(other)) {
There are times when this is not optional, but not here. What is being tested for equality is the current Treet and the one passed in to the compareTo() method.
When the method is called it will be an instance method of a Treet object, say t1. It will be called to compare itself, i.e., t1, to some other Treet object, say t2. And what it does first (after the cast) is see if t1 is equal to t2.