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 trialder ds
Courses Plus Student 87 PointsWhat are we comparing... it is not a number. java is such stu-..
I don't quite understand what the teacher is talking about.. comparing objects.. how can you compare such an abstractive thing. , its size in bytes? what exactly.
3 Answers
jcorum
71,830 PointsCraig is writing a method to compare Tweets, and to fulfill the contract that implementing the Comparable interface requires.
Why override compareTo() for Tweets? There are several reasons, but probably the most important is that you can't sort Tweets unless you do.
When you override the compareTo() method for a class, here Tweets, you can basically do what you want, as long as you return a 0 if they are equal, and a positive or negative int if they are not.
Craig decided to compare them on mCreationDate, and if the dates were equal, then on mDescription.
Some built-in classes, like String, already have a compareTo() method, which allows you to sort them. But for your own classes, you have to provide your own compareTo() method.
Kourosh Raeen
23,733 PointsYou compare the objects by comparing a property of them. Say you have a class representing a blog post with properties like the author of the post, the title of the post, the body of the post, the date of the post, etc. Then you can compare two objects of this type by comparing the dates when they were posted. In another class you may find that a different member variable is suitable as the base of the comparison. For example, the class may represent a movie and you can base the comparison on the length of the movie or the movie's rating.
der ds
Courses Plus Student 87 PointsOne more question, what extacly toString supposed to do ?
Clinton Johnson
28,714 PointsThis returns a String representation of the object that is placed within -> () In other words it is a method that return an object as a String. example: public String toString(#objectGoesHere)
Stephen Shine
5,991 PointsHmmm.... I understand what toString does, but where does that method get used? I can't see where in the code we tell it to convert the object into the string.
Kourosh Raeen
23,733 PointsYou don't explicitly call toString(), but anytime you print the object toString() gets called implicitly. Take a look at this stake overflow link for more detail: http://stackoverflow.com/questions/17051481/how-an-object-will-call-tostring-method-implicitly