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.
// 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); } } |
The integer Value of i = 68
Program 2: For a negative number.
// 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); } } |
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.
// 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); } } |
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
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.