Signup/Sign In
LAST UPDATED: OCTOBER 15, 2020

Java Double longValue() Method

Java longValue() method belongs to the Double class of the java.lang package and is inherited from the Number class. This method returns the long equivalent of the Double-object after a primitive narrowing conversion(Conversion of a higher type to lower type).

In short, this method is used to convert a Double-object into a long value.

Syntax:

public long longValue()  

Parameter:

No parameter is passed in this method.

Returns:

The long equivalent of the Double-object that is created after conversion.

Example 1:

Here, using the longValue() method and the Double-object is converted into its long equivalent.

import java.lang.Double;
public class StudyTonight
{  
    public static void main(String[] args) 
      {  
        //converting Double object into long
          Double x = 125.90;
          long i=x.longValue();
          System.out.println("Long value is " +i);
                   
          Double y = 617.41;  
          long d = y.longValue();  
          System.out.println("Long value is " +d);
      }  
}


Long value is 125
Long value is 617

Example 2:

Here is a user-defined example where anyone using this code can put a value of his choice and get the equivalent long 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 d = sc.nextFloat();  
              Double n = d ;  
              long val = n.longValue();  //converting Double object into long
              System.out.println("Long Value is: " + val);  
           }
         catch(Exception e)
           {
              System.out.println("not a valid double"); 
           }
     }
}


Enter the value to be converted : 56.44
Long Value is: 56
**********************************************
Enter the value to be converted : -885.99
Long Value is: -885
**********************************************
Enter the value to be converted : 0x465
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.



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.