| by Arround The Web | No comments

How to Convert Integer to Int in Java

The terms “int” and “Integer” are used in Java to store the integer type data. The Integer is a wrapper class for creating integer objects defined by java.lang package, while int is a primitive data type that holds a primitive integer value. It saves 32-bit signed two’s complement integers, whereas the Integer object stores its int value in 128 bits. However, there exist chances that you may need the conversion of an Integer object to a primitive data type int.

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:

int y = x;

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”:

Integer x = 11;

Next, we check the type of variable “x” using the “instanceof” operator:

boolean instan = x instanceof Integer;

We will print the value of “x” by using print statement:

System.out.println("x = " + x + " is the instance of Integer class? " + instan);

Now, we simply convert the object of Integer “x” to a primitive type int “y” by using Assignment Operator:

int y = x;

Lastly, print the value of the “y” variable:

System.out.println("int value = "+ y);

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:

x.intValue();

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”:

Integer x = 14;

Print the value of “x” by using print statement:

System.out.println("The value of the instance of Integer class x = "+ x);

Now, convert Integer to primitive type int by calling the method “intValue()”:

int y = x.intValue();

Lastly, print the value of “y”:

System.out.println("int value = " + 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:

Integer x = null;

Print the value of “x” Integer by using print statement:

System.out.println("The value of the instance of Integer class x = "+ x);

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:

int y = (x!=null) ? x.intValue() : 0;

Print the value of “y” int type variable:

System.out.println("int value = " + y);

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:

Integer.parseInt(x.toString());

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”:

Integer x = 5;

We will print the value of “x” by using the “System.out.println()” method:

System.out.println("The value of the instance of Integer class x = "+ x);

Now, we will use “parseInt()” method and passing the Integer object “x” with “toString()” method as an argument:

int y = Integer.parseInt(x.toString());

Finally print the value of “y”:

System.out.println("int value = " + 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.

Share Button

Source: linuxhint.com

Leave a Reply