Register for 45 Day Coding Challenge by CodeStudio And Win Some Exciting Prizes

# Convert Integer To Int in Java

Convert Integer To Int in Java | Here, we will discuss how to convert integer to int in Java. The Integer is a wrapper class whereas the int is the primitive data type in Java. The wrapper class is the way to use primitive data types as an object. Also see:- Int vs Integer Java, Int to Integer in Java

In order to convert Integer to Int we can use the following two different methods:-

1. Auto Unboxing (Direct assignment)
2. Using intValue()

## Convert Integer To Int in Java using Auto Unboxing

Java supports autoboxing and auto unboxing features which means it can convert primitive to wrapper object and wrapper object to primitive respectively itself. We have to just assign the required value.

Hence to convert integer to int in Java using auto unboxing we have to just assign the integer object to the int variable. The below program demonstrates it.

``````public class Main {
public static void main(String[] args) {
Integer num = 50; // auto boxing
System.out.println("Integer: " + num);
System.out.println(num.getClass().getName());

int num1 = num; // auto unboxing
System.out.println("int: " + num1);
}
}``````

Output:-

Integer: 50
java.lang.Integer
int: 50

## Convert Integer To Int in Java Using intValue()

Like other wrapper classes, the Integer class also contains intValue() to convert the wrapper value to an int value. The Integer.intValue() method syntax is as follows:- public int intValue();

Parameters: this does not any parameters
Return type: int
Returns: The converted Integer value.

``````public class Main {
public static void main(String[] args) {
Integer num = 50; // auto boxing
System.out.println("Integer: " + num);
System.out.println(num.getClass().getName());

int num1 = num.intValue();
System.out.println("int: " + num1);
}
}``````

Output:-

Integer: 50
java.lang.Integer
int: 50

## Handling NullPointerException While Converting Integer to int in Java

When the integer object is referenced to a null value than the above two ways will give java.lang.NullPointerException. To handle this, before converting Integer to int we can check whether the Integer object contains null or not.

Convert Integer To Int in Java using Auto Unboxing

``````public class Main {
public static void main(String[] args) {
Integer num = null;
System.out.println("Integer: " + num);

int num1 = (num != null) ? num : 0;
System.out.println("int: " + num1);
}
}``````

Output:-

Integer: null
int: 0

In the above program, it will check whether the given Integer object is null or not. If it is not null then auto unbox the integer object to int value but if it contains null then assign the int primitive variable to 0.

Convert Integer To Int in Java Using intValue()

``````public class Main {
public static void main(String[] args) {
Integer num = null;
System.out.println("Integer: " + num);

int num1 = (num != null) ? num.intValue() : 0;
System.out.println("int: " + num1);
}
}``````

Output:-

Integer: null
int: 0

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!