1
0

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 3 years have passed since last update.

Integerメモ

Posted at

今回はIntegerクラスのメソッドの使い方について学んでいきます。
随時更新予定です。

compare(int x, int y)

2つのint値を数値的に比較します。
x == yの場合は値0、
x < yの場合は0より小さい値、
x> yの場合は0より大きい値

System.out.println(Integer.compare(1, 2));
System.out.println(Integer.compare(2, 2));
System.out.println(Integer.compare(5, 2));
-1
0
1

compareTo(Integer anotherInteger)

2つのIntegerオブジェクトを数値的に比較します。

このIntegerが引数Integerと等しい場合は値0。
このIntegerが引数Integerより小さい数値の場合は0より小さい値。
このIntegerが引数Integerより大きい数値の場合は0より大きい値。

Integer number = new Integer(100);

System.out.println(number.compareTo(10));
System.out.println(number.compareTo(500));
System.out.println(number.compareTo(100));
1
-1
0

decode(String nm)

StringをIntegerにデコードします。

Integer num = Integer.decode("100");
Integer nm = 100;

System.out.println(num + nm);
200

equals(Object obj)

このオブジェクトを指定されたオブジェクトと比較します。

Integer number = new Integer(100);
Integer num = new Integer(100);
Integer nm = new Integer(500);

System.out.println(number == num);
System.out.println(number.equals(num));
System.out.println(number.equals(nm));
false
true
false

parseInt(String s)

文字列の引数を符号付き10進数の整数型として構文解析します。
戻り値がint型

String str = "123";
int i = Integer.parseInt(str);
int sum = i + 100;

System.out.println(sum);
223

valueOf(int i),valueOf(String s)

指定されたint値を表すIntegerインスタンスを返します。
指定されたStringの値を保持するIntegerオブジェクトを返します。
戻り値がInteger型

int i1 = Integer.valueOf(100);
int i2 = Integer.valueOf("100");

System.out.println(i1 + i2);
200
1
0
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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?