Printing
It is using the toString()
method to print each DVDInfo
object.
The println
method will do a String.valueOf()
on its parameter. Then the println
code will print the resulting string.
The parameter you pass println
is a List
and it has a toString()
method of its owh that inserts square braces and commas and calls toString()
on the elements, putting the result in between. This produces a string starting with a [ and ending with a ]. Inside those braces are the results of calling the toString()
method on the DVDInfo
objects and the commas.
That's how it prints.
Sorting
The collections sort()
method knows how to traverse the collection and compare the elements to put them into order. It uses standard sorting algorithms to do so. But you don't need to know the algorithm, just that it does the sort. This is the essence of encapsulation
. Classes should do one thing and let other classes do their one thing and everything works better.
Inside the sort algorithm, it calls compareTo
and then does if
statements to either reorder two elements so the the lower one is first or leave the two elements so the lower one is first. It depends on whether they were in order before the sort. Equal can go either way--move or don't. But the sort algorithm gets to decide.
Notes
You might find it interesting to download the source for the Oracle java libraries and connect your IDE to the source so you can click (or whatever) and view the source for the various standard library functions like println
and sort
. Its sometimes too tedious but sometimes very interesting.