Chris Rubio1,643 Points
Please explain the compareTo(obeject obj) method that craig is using.
Ok this java course has really came to a point where i am not comprehending anything of whats going on, if someone would be kind enough to explain the compareTo method that craig is using, why is he using it ? what is he trying to do with it ? please feel free to ellaborate as if talking to a 7 year old, i do not mind, the SIMPLER the better to be honest
Oleksandr Prygara1,113 Points
What we are trying to do in this video is to SORT the treets or, in other words, put them in some order.
- Question: what kind of order??? As the treets that we have created are the instances of the classes also created by us (we created the blueprint for this object), it is up to us to say how we should SORT them. The system has no clue how to SORT them... However, if we want to SORT some Strings, this object (I mean String) was created by the guys who developed the language itself, so they wrote the rule for SORT method in advance. Since they decided to SORT the Strings alphabetically, "ananas" will always be in front of "banana", though I prefer bananas.
-Question: how does the system sort the objects?? To SORT the objects, the system compares them with each other and defines which one goes first. Since the java creators decided to SORT the strings alphabetically (and not according to what I prefer to eat), ananas will always be before bananas because 'a' comes before 'b'. Now with Treets, the system cannot SORT them because it does not know how to compare two treets. That's why we need to tell it which criteria should be taken into account when comparing them. Apparently, Graig wants to compare the creation dates because he wants to sort them by this criterium. As for me, I would prefer to SORT them alphabetically by the first letter of Author (pretty much the same as the guys from Java development team did for Strings). But it is up to each and every one of us to decide on the criteria of comparison. That's why we need to override the method so that when we use sort(), it will SORT the objects by comparing them according to the criteria we want.
- Question: ok, but why we use the compareTo() again with the dates, aren't we overriding it?? It comes from method overloading chapter. Recall, the methods are the same if their names and what they accept as the parameters type are the same. Should two methods have the same name but accept different parameters, these are two different methods (though with the same name). It is like taking two guys with the same name and say that they are basically one person - no way. Jimmy from California is not the same Jimmy from Texas. These are two different people. So, coming to the point, we are overriding the compareTo() in order to tell the system how to compare our Treets. And at one moment we say:
- "You know what, the system, I want you to compare my treets by the Creation Date, so that when I call sort(), you will compare their Creation Dates and put my tweet in order from the most oldest to the newest ones. Got it?". And the system will say:
- "Yeah, bros, no worries, I will do that." But then we ask:
- "Wait, but do you know how to compare the dates??" And the system will say:
- "Of course, those guys from Java told how to."
That's why when we write mCreationDate.compareTo(other.mCreationDate), the system recognizes the type of DATE and it knows how to compare it.
Question: Why then should we say that our Treet class implements the Comparable interface?? As Graig said, the interface is basically a contract that you sign. In this contract you will see that it obliges you to use its template. So, when other programmers deal with your class, they know that you have signed the contract, so basically your class includes the minimum requirements defined in this contract. The minimum requirement of Comparable interface (as much as I know), it is to use compareTo() method. You may override it or just leave it like it is, but you have to define it in you class by writing "public int compa...", you know the rest.
Question: what is "(equals(other))" I am less sure on but it basically compares two instances of your class. So, the first instance is the one you call compareTo() function from and another is the one that you passed inside the compareTo( here ) function. We can also write it in this way: this.equals(other), meaning that "this" relates to the instance of the class that you operate with. It might be a bit confusing but you have to imagine the process like the following:
You are in the gym and suddenly, you decide to see if your your biceps is as big as Schwartzenegger's ones. So, inside your head you call an equals() function. When you approach your biceps to the photo of Schwartzenegger's biceps, you compare it by inputing a parameter (size of the biceps) into your mental equals() method, just right in between the brackets. But you don't write myBicepts.equals(SchwartzeneggerBicepts), you just write: equals(SchwartzeneggerBicdepts), because you know that you compare it to "this." or your biceps.
Basically that's how I understand the video, sorry for the long answer, but I hope it explains what you wanted to clarify.