Integer memo

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

Integer memo
docker memo
Lombok memo
Dockerfile memo
Iterator memo
corretto memo
Java memo
AWS memo
Dcokerfile memo
Ruby memo
Memo Stream
Spring retrospective memo
docker tutorial (memo)
java anything memo
primary key memo
Stream API memo
Docker operation memo
Eclipse trick memo
Java Silver memo
java, maven memo
Compare Integer with ==
JavaParser usage memo
Docker command memo
SpringBoot-JPA-Hibernate addictive memo
Kubernetes/container-related standardization memo
Java SE 7 memo
[Memo] docker summary
OpenJDK 11 installation memo
MySQL migration memo (1)
WatchService usage memo
PlantUML usage memo
java anything memo 2
Docker-compose command memo
e-Gov setting memo
[wip] Ruby memo
Maven3 error memo
Java specification memo
JUnit5 usage memo
Java pattern memo