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
edmond habimana
Courses Plus Student 8,352 PointsI am so confuse to the point where my head is about to explode.
Ok, so this piece of code is throwing me off.
if(equals(other)){
return 0;
}
I read in the forum where people say we are comparing Treet to Treet class but even then i am still confuse, when we are trying to find out if they are equal are we looking at fields or the objects treet vs secondTreet.
Thanks.
5 Answers
gyorgyandorka
13,811 PointsHi! equals() is a method of the Object class, and every type inherits it, including our Treet class (we can override the default implementation of course). So equals() is readily usable by our Treet instances (without having to be defined in the Treet class), and equals(other) is equivalent to this.equals(other). Inside the class definition, we do not need to use this explicitely if no confusion arises without it.
The equals() method compares the object on which it was called with the object passed in as argument. So, we are comparing this particular instance (a Treet object) to another object (other). The variable other was originally the parameter in our compareTo() method (in which we have this conditional block you've highlighted), so anything we pass in as argument to compareTo() will become the argument of our equals() method (and it will compare this Treet instance with that particular object).
We implemented the compareTo() method (and by doing so, the Comparable interface) in the Treet class so that later we could use the Arrays.sort() method on treets (which is an array of Treet objects containing - at the moment - Treet and secondTreet). Arrays.sort() orders the elements of the array passed in by calling the elements' compareTo() method (which in turn - at least in our implementation in the Treet class - calls the equals() method to check first if the two objects are equal before checking the date, description and author), so it will eventually call equals() on Treet to compare it to secondTreet.
I put the whole code here for others reading this post for reference:
public class Treet implements Comparable<Treet> {
...
@Override
public int compareTo(Treet other) {
if (equals(other)) { // same as this.equals(other)
return 0;
}
int dateCmp = creationDate.compareTo(other.creationDate);
int descriptionCmp = description.compareTo(other.description);
int authorCmp = author.compareTo(other.author);
if (dateCmp == 0) {
if (descriptionCmp == 0) {
return authorCmp;
}
return descriptionCmp;
}
return dateCmp;
}
...
}
Btw. the final part can be refactored, much nicer this way:
if (dateCmp != 0) return dateCmp;
if (descriptionCmp != 0) return descriptionCmp;
return authorCmp;
(Note: I've combined my previous responses in this updated answer.)
Douglas North
10,884 PointsI refused to continue the course till i got this, this discussion was very helpful, thanks guys.
gyorgyandorka
13,811 PointsGlad to hear it! Craig is awesome, but this course definitely needs an overhauling.
edmond habimana
Courses Plus Student 8,352 PointsWhen you say "we are comparing this particular instance (a Treet object) to another object (other)" do you mean comparing treet to secondTreet.
Again thanks for the help.
gyorgyandorka
13,811 PointsEdit: I removed my answer to this question and updated the first one, everything is there in one place.
edmond habimana
Courses Plus Student 8,352 PointsI understand it now, thank for the help!
Timothy Hilley
2,292 Pointshey yea, thanks man, i was the same as douglas and this was what was throwing me for a loop
Garvit Kashyap
2,918 PointsGarvit Kashyap
2,918 PointsCould you explain this part of the code to me? How does putting "other" in front of one mCreationDate compare the two different creation dates? How does the computer differentiate between the two different mCreationDates or mAuthors or mDescription? Please help.
gyorgyandorka
13,811 Pointsgyorgyandorka
13,811 PointsGarvit Kashyap
creationDate.compareTo(other.creationDate)is the same asthis.creationDate.compareTo(other.creationDate). Inside the class definition, we do not need to usethisexplicitely if no confusion arises without it.thisrefers to the particular class instance (in this case aTreetobject) on which we will call thecompareTo()method. Theotherobject is an object we should pass in as argument to thecompareTo()method of theTreetclass, it is given as a parameter in the method signature.Note: kinda obvious, but it's worth pointing out that the
compareTo()methods in these particular lines are not the same as the "outer" method we're talking about right now (the one implemented for theTreetclass), i.e. we're not referring to the method in some recursive way, since these are notTreetobjects. TheTreetclass'scompareTo()method calls the attribute's owncompareTo()implementation on each of them, to compare these different attributes of the twoTreetobjects in some specific order, and based on this, return us a comparison value for the wholeTreetobject (creationDateis aDatetype,description&authorareStringtypes, so on the first line whencompareTo()is called on thecreationDateattribute it is the version implemented for theDatetype, on the second and third line, when we're calling it on thedescriptionandauthorattributes [which are strings] it is thecompareTo()method defined in theStringtype).Garvit Kashyap
2,918 PointsGarvit Kashyap
2,918 PointsThanks so much!