This time we will learn how to use the methods of the Integer class. It will be updated from time to time.
compare(int x, int y) Numerically compare two int values. Value 0 for x == y, A value less than 0 if x <y, Greater than 0 if x> y
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) Numerically compare two Integer objects.
Value 0 if this Integer is equal to the argument Integer. A value less than 0 if this Integer is less than the argument Integer. A value greater than 0 if this Integer is greater than the argument Integer.
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) Decodes a String to an Integer.
Integer num = Integer.decode("100");
Integer nm = 100;
System.out.println(num + nm);
200
equals(Object obj) Compares this object with the specified object.
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) Parses the string argument as a signed decimal integer type. Return value is int type
String str = "123";
int i = Integer.parseInt(str);
int sum = i + 100;
System.out.println(sum);
223
valueOf(int i),valueOf(String s) Returns an Integer instance that represents the specified int value. Returns an Integer object that holds the value of the specified String. Return value is Integer type
int i1 = Integer.valueOf(100);
int i2 = Integer.valueOf("100");
System.out.println(i1 + i2);
200
Recommended Posts