Signup/Sign In

Java Float intValue() Method

Java intValue() method belongs to the Float class of the java.lang and is inherited from the Number class. It is an instance method that returns the int equivalent of the Float 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 Float object into an integer value.

Syntax:

public int intValue()  

Parameter:

No parameter is passed in this method.

Returns:

The integer equivalent of the Float object that is created after conversion.

Example 1:

Here, using the intValue() function, the Float object is converted into its primitive int equivalent.

import java.lang.Float;
public class StudyTonight
{  
    public static void main(String[] args) 
    {  
        //Converting Float object into int
        Float x = 56.667f;
        int i = x.intValue();
        System.out.println(i);
    
        Float y = -90.0f;  
        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 float 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);  
              float i = sc.nextFloat();  
              Float n = i ;  
              int val = n.intValue();  //converting Float object into int
              System.out.println("Integer Value is: " + val);  
           }
           catch(Exception e)
           {
              System.out.println("not a valid float"); 
           }
    }
}


Enter the value to be converted : 78.433
Integer Value is: 78
***********************************************
Enter the value to be converted : -73.8
Integer Value is: -73
***********************************************
Enter the value to be converted : 0x77.9
not a valid float

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.



About the author:
A Computer Science and Engineering Graduate(2016-2020) from JSSATE Noida. JAVA is Love. Sincerely Followed Sachin Tendulkar as a child, M S Dhoni as a teenager, and Virat Kohli as an adult.