This write-up will illustrate the methods for converting the Integer to int in Java.
How to Convert Integer to int in Java?
In Java, you can utilize the following methods for converting Integer to int:
- Assignment operator
- intValue() method
- parseInt() method
We will now check out the working of each of the mentioned methods one by one!
Method 1: Convert Integer to int in Java Using Assignment Operator
Converting Integer to int using assignment operator “=” is implicit type conversion. It is the simple and easiest way to convert Integer to int.
Syntax
The syntax of converting Integer to int is given below:
Here, “x” is the object of the “Integer” class which will be converted to int “y” using the “=” assignment operator.
Example
First of all, we will create an Integer object “x” that contains the integer value “11”:
Next, we check the type of variable “x” using the “instanceof” operator:
We will print the value of “x” by using print statement:
Now, we simply convert the object of Integer “x” to a primitive type int “y” by using Assignment Operator:
Lastly, print the value of the “y” variable:
The output shows the successful conversion of Integer to int:
Note: For Java version 1.5 or above, you can perform Integer to int conversion using implicit conversion. However, for Java version 1.4 or lower same operation have to be performed using explicit conversion.
Method 2: Convert Integer to int in Java Using intValue() method
For converting Integer to int in Java explicitly, you can utilize the “intValue()” method of the Java “Integer” class. It takes no arguments and gives a primitive value as an output.
Syntax
The intValue() method has the following syntax:
Here, the “intValue()” method is called with an Integer type object “x”. The specified method will convert Integer x to int.
Example 1
Create an Integer object “x” with value “14”:
Print the value of “x” by using print statement:
Now, convert Integer to primitive type int by calling the method “intValue()”:
Lastly, print the value of “y”:
As you can see, the intValue() method returned the required int value:
There can be a situation where the Integer object you want to convert has a “null” value. What will happen in such a situation? The below-given example will let you know about that.
Example 2
In this example, the Integer object “x” is assigned a “null” value:
Print the value of “x” Integer by using print statement:
Here, we use the ternary operator to check whether the object is null or not; if null, then assign any default value that will be returned as an int type by calling the “intValue()” method:
Print the value of “y” int type variable:
Here, the output shows that the Integer object has a null value which is converted to “0”:
Let’s see another method for explicit conversion of Integer to int.
Method 3: Convert Integer to int in Java Using parseInt() Method
There is another method of the Integer class called “parseInt()” that is also used to convert Integer to int. In this method, a string is accepted as an argument and gives an int value as an output.
Syntax
The following describes the syntax for the “parseInt()” method:
Here, the “x” Integer object is first turned to a string, which is then parsed as a “int” with the “parseInt()” method..
Example
In this example, we have an integer value “5” that is stored in Integer object “x”:
We will print the value of “x” by using the “System.out.println()” method:
Now, we will use “parseInt()” method and passing the Integer object “x” with “toString()” method as an argument:
Finally print the value of “y”:
Output
We have compiled all the methods for converting Integer to int in Java.
Conclusion
There are two ways for converting an integer to an integer: implicit conversion and explicit conversion. Java version 1.5 and above follow the implicit conversion, while Java version 1.4 and lower support explicit conversion. You can use the Assignment operator to convert Integer to int implicitly. While parseInt() and intValue() methods are utilized for explicit Integer to int conversion. This write-up illustrated the methods for converting Integer to int in Java.