LAST UPDATED: AUGUST 26, 2020
Java Double intValue() method
Java intValue()
method belongs to the Double
class of the java.lang
and is inherited from the Number
Class. It is an instance method that returns the int equivalent of the Double-object after a narrowing primitive conversion(Conversion of a higher data type into a lower data type).
In short, this method is used to convert a Double object value into a primitive integer value.
Syntax:
public int intValue()
Parameter:
No parameter is passed in this method.
Returns:
The integer equivalent of the Double-object that is created after conversion.
Example 1:
Here, using the intValue()
function, the Double-object is converted into its integer equivalent.
import java.lang.Double;
public class StudyTonight
{
public static void main(String[] args)
{
//converting Double object into int
Double x = 56.667;
int i = x.intValue();
System.out.println(i);
Double y = -90.0;
int d = y.intValue();
System.out.println(d);
}
}
56
-90
Example 2:
Here is a user-defined example where anyone using this code can put a value of his choice and get the equivalent double value.
import java.util.Scanner;
public class StudyTonight
{
public static void main(String[] args)
{
try
{
System.out.print("Enter the value to be converted : ");
Scanner sc = new Scanner(System.in);
double i = sc.nextDouble();
Double n = i ;
int val = n.intValue(); //converting Double object into int
System.out.println("Integer Value is: " + val);
}
catch(Exception e)
{
System.out.println("not a valid double");
}
}
}
Enter the value to be converted : 78.943
Integer Value is: 78
************************************************
Enter the value to be converted : -098.543
Integer Value is: -98
************************************************
Enter the value to be converted : 0x678
not a valid double
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.