Comparisons and Comparables

Python Java
The comparison operators are ==, !=, <, >, <=, and >=.

All comparison operators return True or False.

Only objects that are comparable, such as numbers and sequences of comparable objects (strings and lists of numbers or strings), can be operands for comparisons.

Example:

print(“AAB” > “AAA”)

The comparison operators are ==, !=, <, >, <=, and >=.

All comparison operators return True or False.

All values of primitive types are comparable.

Values of reference types are comparable if and only if they recognize the compareTo method. compareTo returns 0 if the two objects are equal (using the equals method), a negative integer if the receiver object is less than the parameter object, and a positive integer if the receiver object is greater than the parameter object. Classes of comparable objects generally implement the Comparable interface, which includes the compareTo method.

Example:

String s = “AAB”;
System.out.println(s.compareTo(“AAA”) > 0);

© Ken Lambert. All rights reserved.