[Java] compareTo

What is compareTo

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

[Java] compareTo
Java
Java
Java learning (0)
Studying Java ―― 3
[Java] array
[Java] Annotation
[Java] Module
Java array
Java tips, tips
Java methods
Java method
java (constructor)
Java array
[Java] ArrayDeque
java (override)
java (method)
Java Day 2018
java (array)
Java static
Java serialization
JAVA paid
Java (set)
java shellsort
Studying Java -5
java reflexes
java (interface)
Java memorandum
☾ Java / Collection
Java array
Studying Java ―― 1
[Java] Array
[Java] Polymorphism
Studying Java # 0
Java review
java framework
Java features
[Java] Inheritance
FastScanner Java
Java comparison using the compareTo () method
Java features
java beginner 3
Java memo
java (encapsulation)
Java inheritance
[Java] Overload
Java basics
Decompile Java
[Java] Annotation
java notes
java beginner
Java (add2)
JAVA (Map)
[java] interface
Java9 collection
Java basics
Java methods
Java diary
Java inheritance
[Java] enum
[Java] FizzBuzzCounter