The
int compareTo(Object other) method is indeed from the Comparable interface. The
equals() method comes from class Object, the superclass of all classes in
Java. Note that because
equals() is in the Object class, all objects in Java always have an
equals() method (if you don't declare one yourself in your class, it will inherit it from class Object). But not all objects have a
compareTo() method; they will only have that if the class (or a superclass) implements the Comparable interface.
Type casting is a way to tell the compiler "look, I have an object of type X here, but I want you to treat it as type Y. Don't check it, just trust me!". What it essentially does is deferring type checking from compile time to runtime - when, while you run the program, it turns out that the object that you cast cannot be treated as type Y, you will get a ClassCastException.
Line 4: Yes, you're declaring a variable named
other, of type
CD, and then you cast
otherObject to the type
CD so that you can assign it to
other.
Line 22: You're creating a new array of which the length is the length of
collection + 100.
Line 25: I don't know what you mean by "moving the array indexes". What that line does is copy the content of the array
collection to the array
temp.
Line 27: You simply make the variable
collection refer to the array that
temp refers to (so, you're replacing the old array that
collection referred to with the new, larger array).
Remember that in Java, variables of non-primitive types are
references. So
temp and
collection are not the arrays themselves, they are variables that refer to arrays. Assigning a value to such a variable makes it refer to another array.