1. Using intValue() method
2. Using parseInt() method
3. Using assignment operator(Implicit conversion)
Read Also: How to convert int to Integer in Java
Let's dive deep into the topic:
Convert Integer to int in Java
1. Using intValue() method
According to Oracle docs, we can convert Integer object to primitive data type int using Integer's class intValue() method. You can find the example below:
public class IntegerToInt {
public static void main(String args[]) {
Integer var = new Integer(100);
System.out.println("Integer value is: "+var);
int var2 = var.intValue();
System.out.println("int value is: "+var2);
}
}
Output:
Integer value is: 100
int value is: 100
2. Using parseInt() method
parseInt() method can also be used to convert Integer object to int primitive data type. It takes a String argument and returns an int value. Please make sure we have a String integer object only as shown in the example below:
public class IntegerToInt2 {
public static void main(String args[]) {
Integer var = new Integer(1000);
System.out.println("Integer value is: "+ var);
int var2 = Integer.parseInt(var.toString());
System.out.println("int value is: "+ var2);
}
}
Output:
Integer value is: 1000
int value is: 1000
3. Using assignment operator(Implicit conversion)
This is the simplest way to convert Integer object to int primitive data type in Java. We will not use any explicit casting or method instead we will use the assignment operator and the conversion takes place as shown in the example below:
public class IntegerToInt3 {
public static void main(String args[]) {
Integer var = new Integer(10000);
System.out.println("Integer value is: "+ var);
int var2 = var;
System.out.println("int value is: "+ var2);
}
}
Output:
Integer value is: 10000
int value is: 10000
That's all for today, please mention in the comments in case you know any other way to convert Integer to int primitive data type in Java.