Contents
Page-10
Prev
Next
Page+10
Index
Comparison in Java
The various types of Java are compared in different ways:
- The primitive types int, long, float and
double are compared using < and >. They cannot
use .compareTo() .
- String uses .compareTo(); it cannot use < and >.
.compareTo() for String is case-sensitive.
- An application object type can be given a .compareTo() method,
but that allows only one way of sorting. We might want a case-insensitive
comparison for String.
- A Comparator can be passed as a function argument, and this
allows a custom comparison method.
The Java library has a case-insensitive Comparator for String.
Sometimes it is necessary to implement several versions of the same method
in order to use different methods of comparison on its arguments.