Signup/Sign In

Java Integer intValue() Method

Java intValue() method belongs to the Integer class of the java.lang and is inherited from the Number class. It is an instance method that returns the int equivalent of the specified number.

It is generally an Unboxing method. Although after the introduction of Java 5, the conversion is done automatically(autoboxing), but it is important for you to know the concept of Boxing and Unboxing.

Unboxing: Conversion of an Integer to int is referred to as the unboxing

Boxing: Conversion of an int to Integer is referred to as boxing.

Syntax:

public int intValue()  

Parameter:

No parameter is passed in this method.

Returns:

the numeric value of the object after it is converted into an int (primitive).

Exception:

The exception that can be encountered in this method is NullPointerException.

Example 1:

Here, using the intValue() function, the integer object are converted into its numerical int equivalent.

import java.lang.Integer;

public class StudyTonight 
{  
    public static void main(String[] args) 
    {                  
          Integer n1 = 25;  
          Integer n2 = 46;
           
          int i = n1.intValue(); // returns the value of the Integer object n1 as an int
          int j = n2.intValue(); // returns the value of the Integer object n2 as an int
          
          System.out.println("int value is " + i);  
          System.out.println("int value is " + j); 
           
        }  
}


int value is 25
int value is 46

Example 2:

Here is a user-defined example where anyone using this code can put a value of his choice and get the equivalent int value.

import java.util.Scanner;  

public class StudyTonight
{  
    public static void main(String[] args)
    {                  
        try
        {
           System.out.print("Enter The Value ");  
           Scanner sc = new Scanner(System.in);  
           int i = sc.nextInt();  
           Integer n = i; 
           System.out.println("Integer Value is: " + n.intValue()); //Converting the Integer object into int  
        }
        catch(Exception e)
        {
           System.out.println(" Invalid Input!!");
        }
   }
}


Enter The Value 38
Integer Value is: 38
***************************
Enter The Value studyjava
Invalid Input!!

Live Example:

Here, you can test the live code example. You can execute the example for different values, even can edit and write your examples to test the Java code.



About the author:
A Computer Science and Engineering Graduate(2016-2020) from JSSATE Noida. JAVA is Love. Sincerely Followed Sachin Tendulkar as a child, M S Dhoni as a teenager, and Virat Kohli as an adult.