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 trialClay Janson
96 Pointscan anyone explain me this code plzz
@Override public int compareTo(Object obj){ Treet other = (Treet) obj; if(equals(other)){ return 0; } int datecmp = mCreationDate.compareTo(other.mCreationDate); if (datecmp == 0){ return mDescription.compareTo(other.mDescription); } return datecmp; }
2 Answers
Hassaan Ahmad
1,979 PointsI'm new to these concepts but I'll try to explain the code according to my knowledge.
So,in the given code, you're overriding a method that has been declared in the Comparable interface. This allows you to use it as per your need. In the method body, you're casting the passed in object to the Treet class as you already know that it's a treet otherwise you'd have used the instanceof keyword to check if it is a treet or not. First, the casted object is compared to passed in treet, if both the objects are same then they are the same treets and it returns 0. If that's not the case,then the creation dates of both treets are compared. Now there is a possibility that two different treets might have the same creation dates, so if there creation dates are the same, we check them for there descriptions. If there descriptions are also the same,then they must be the same treets, and we return 0, else we just return the comparison result. Finally, if no return commands are executed, we just return the creation date comparison to sort out the treets.
sudhichai ungsuthornrungsi
7,634 PointsYes but why the compare on are return -1 since in this method it's override and it's only possible to return as 0
Patrik Horváth
11,110 PointsPatrik Horváth
11,110 Pointsyou should go true JAVA basics its clearly described in it or JAVA 8 book, because here you have ANOTATION @overide with telling you there is method witch this name so i need somehow tell program override it with this new method with same name. also (Treet) obj is type of castable its a lot of hard to underestend it there is another issiu with it i mean " instanceOf" also Interface maybe Abstract class used here
Go True Basics ( this is more advenced cause you should learn it if you already underestend OOP