Java中的整数intValue()方法
存在于Java.lang 包中的 Integer 类的intValue()是Java中的一个内置方法,它将该整数的值作为继承自Number Class的 int 返回。包视图如下:
--> java.lang Package
--> Integer Class
--> intValue() Method
句法:
public int intValue()
返回类型:转换为整数类型后由对象表示的数值。
Note: This method is applicable from java version 1.2 and onwards.
现在我们将涵盖不同的数字,例如正数、负数、小数,甚至是字符串。
- 对于一个正整数
- 对于负数
- 对于十进制值和字符串
案例1:对于一个正整数
例子:
Java
// Java Program to Illustrate
// the Usage of intValue() method
// of Integer class
// Importing required class/es
import java.lang.*;
// Main class
public 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);
}
}
Java
// Java program to illustrate the
// use of intValue() method of
// Integer class of java.lang package
// Importing required classes
import java.lang.*;
// Main class
public 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);
}
}
Java
// Java Program to illustrate
// Usage of intValue() method
// of Integer class of java.lang package
// Importing required classes
import java.lang.*;
// Main class
class 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);
}
}
输出:
The integer Value of i = 68
案例 2:对于一个负数
例子:
Java
// Java program to illustrate the
// use of intValue() method of
// Integer class of java.lang package
// Importing required classes
import java.lang.*;
// Main class
public 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
// Java Program to illustrate
// Usage of intValue() method
// of Integer class of java.lang package
// Importing required classes
import java.lang.*;
// Main class
class 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);
}
}
输出:
Note: It returns an error message when a decimal value is given. For a string this works fine.