A convenient method that determines (in a dictionary sense) which of the two variables is larger. It is also implemented in String type and Integer type, so it is easy to use.
Ex. A.compareTo (B)
| Value to return | |
|---|---|
| A < B | negative |
| A = B | 0 |
| A > B | Positive |
qiita.java
public class Main{
public static void main(String[] args) {
int a = 400;
Integer b = 10000;
System.out.println("Compare1: " + "ABC".compareTo("XBD"));
System.out.println("Compare2: " + "DDD".compareTo("DDD"));
System.out.println("Compare3: " + b.compareTo(a));
}
}
Execution result:
Compare1: -23
Compare2: 0
Compare3: 1
Recommended Posts