Integer intValue() Method in Java

The java.lang.Integer.intValue() is an inbuilt method in java that returns the value of this integer as an int.

Syntax :

public int intValue()

Parameters: The method does not accept any parameters.

Return Value : The method returns the numeric value which is represented by the object after conversion to the integer type.

Below programs illustrate the java.lang.Integer.intValue() method:
Program 1: For a positive integer.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java praogram to illustrate the use of
// java.lang.Integer.intValue() method
import java.lang.*;
  
public class Geeks {
  
    public static void main(String[] args)
    {
  
        Integer intobject = new Integer(68);
  
        // Returns the value of this Integer as an int
        int i = intobject.intValue();
        System.out.println("The integer Value of i = " + i);
    }
}

chevron_right


Output:

The integer Value of i = 68

Program 2: For a negative number.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java praogram to illustrate the use of
// java.lang.Integer.intValue() method
import java.lang.*;
  
public class Geeks {
  
    public static void main(String[] args)
    {
  
        Integer intobject = new Integer(-76);
  
        // Returns the value of this Integer as an int
        int i = intobject.intValue();
        System.out.println("The integer Value of i = " + i);
    }
}

chevron_right


Output:

The integer Value of i = -76

Program 3: For a decimal value and string.
Note:It returns an error message when a decimal value and string is given.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java praogram to illustrate the use of
// java.lang.Integer.intValue() method
import java.lang.*;
  
public class Geeks {
  
    public static void main(String[] args)
    {
  
        Integer intobject = new Integer(98.22);
  
        int i = intobject.intValue();
        System.out.println("The integer Value of i = " + i);
  
        Integer intobject = new Integer("52");
  
        int i = intobject.intValue();
        System.out.println("The integer Value of i = " + i);
    }
}

chevron_right


Output:

prog.java:9: error: no suitable constructor found for Integer(double)
    Integer intobject = new Integer(98.22);
                        ^
    constructor Integer.Integer(int) is not applicable
      (argument mismatch; possible lossy conversion from double to int)
    constructor Integer.Integer(String) is not applicable
      (argument mismatch; double cannot be converted to String)
prog.java:14: error: variable intobject is already defined in method main(String[])
        Integer intobject = new Integer("52");
                ^
prog.java:17: error: variable i is already defined in method main(String[])
    int i = intobject.intValue();
        ^
3 errors


My Personal Notes arrow_drop_up

lets make it a lil simple

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.