Java Integer intValue()方法
java.lang包中Integer类的intValue()是java中的一个内置方法,它可以将这个整数的值作为一个int来返回,它继承自 Number类。 该包的视图如下。
--> java.lang Package -- > Integer Class --> intValue() Method
语法
public int intValue()
返回类型: 转换为整数类型后的对象所代表的数字值。
注意: 该方法从java 1.2版及以后适用。
现在我们将涉及不同的数字,如正数、负数、小数,甚至是字符串。
对于一个正整数对于一个负数对于一个小数值和字符串案例1: 对于一个正整数
例子
// Java Program to Illustrate// the Usage of intValue() method// of Integer class // Importing required class/esimport java.lang.*; // Main classpublic class GFG { // Main driver method public static void main(String[] args) { // Creating object of Integer class inside main() Integer intobject = new Integer(68); // Returns the value of this Integer as an int int i = intobject.intValue(); // Printing the value above stored in integer System.out.println("The integer Value of i = " + i); }}输出
The integer Value of i = 68
案例2: 对于一个负数
例子
// Java program to illustrate the// use of intValue() method of// Integer class of java.lang package // Importing required classesimport java.lang.*; // Main classpublic class GFG { // Main driver method public static void main(String[] args) { // Creating an object of Integer class Integer intobject = new Integer(-76); // Returns the value of this Integer as an int int i = intobject.intValue(); // Printing the integer value above stored on // console System.out.println("The integer Value of i = " + i); }}输出
The integer Value of i = -76
案例3: 对于一个十进制的数值和字符串。
例子
// Java Program to illustrate// Usage of intValue() method// of Integer class of java.lang package // Importing required classesimport java.lang.*; // Main classclass GFG { // Main driver method public static void main(String[] args) { // Creating an object of Integer class Integer intobject = new Integer(98.22); // Using intValue() method int i = intobject.intValue(); // Printing the value stored in above integer // variable System.out.println("The integer Value of i = " + i); // Creating another object of Integer class Integer ab = new Integer("52"); int a = ab.intValue(); // This time printing the value stored in "ab" System.out.println("The integer Value of ab = " + a); }}输出

注意: 当给出一个十进制值时,它会返回一个错误信息。对于一个字符串,它可以正常工作。
