1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

[Java]compareTo

Posted at

compareToとは

2つの変数のどちらが大きいかを(辞書的な意味で)判断してくれる便利メソッド。
String型やInteger型にも実装されているので、お手軽に使えちゃう。

Ex. A.compareTo(B)とすると、

   返す値
A < B
A = B 0
A > B
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));
	}
}

実行結果:

Compare1: -23
Compare2: 0
Compare3: 1
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?