1.5 KiB
1.5 KiB
- #CT2106 - Object-Oriented Programming
- Previous Topic: Static Fields & Exceptions
- Next Topic: No Next Topic.
- Relevant Slides:
-
Natural Ordering
- When deciding on whether one object is greater than or less than another, we refer to the natural ordering of the object's class.
- Natural ordering is the ordering imposed on an object when its class implements the Comparable interface.
-
public interface Comparable<T>
- This interface imposes a total ordering on the objects of each class that implements it.
- The class's
compareTo()
method is referred to as its natural comparison method. - Lists (and arrays) that implement this interface can be sorted automatically by
Collections.sort
. - Objects that implement this interface can be used as keys in a sorted map or as elements in a sorted set, without the need to specify a comparator.
- The
<T>
inComparable<T>
means that we can specify in advance the types of the object that should be compared. - It returns a negative integer, zero, or a positive integer depending on whether the object is less than, equal to, or greater than the specified object.
-
-
assert
- Use
assert
to declare that a statement must be true.- If it is not true, your program will throw an
AssertionError
Exception. - You can use the
assert
statement as a quick way to test for expected output. -
assert(2==2);
- If it is not true, your program will throw an
- Use