java.lang.Double longValue()

Syntax:

public long longValue()

Java Code Example :

This java example source code demonstrates the use of longValue() method of Double class.

Some takeaways on the longValue()  method:

  • returns the value of this Double as a long after a narrowing primitive conversion.
  • specified by longValue in class Number
package com.javatutorialhq.java.examples;

/*
 * This example source code demonstrates the use of  
 * longValue() method of Double class
 */

public class DoubleLongValue {

	public static void main(String[] args) {

		/*
		 * As you can see on the value of result 
		 * variable would be the maximum
		 * value that primitive long would be able
		 * to hold
		 */
		System.out.println("Maximum value of Long is :" 
				+ Long.MAX_VALUE);
		Double val = 10337203685477580700.1321;
		long result = val.longValue();
		System.out.println("Double " + val + " long value=" 
				+ result);

		/*
		 * Both the variable anotherResult and testResult 
		 * will yield to the same value since the long 
		 * primitive doesn't have any decimal places, thus
		 * the decimal values will be truncated
		 */

		Double anotherValue = 65.3;
		long anotherResult = anotherValue.longValue();
		System.out.println("Double " + anotherValue + " long value="
				+ anotherResult);

		Double testValue = 65.0;
		long testResult = testValue.longValue();
		System.out.println("Double " + testValue + " long value=" 
				+ testResult);

	}

}

Sample Output :

Running the above example source code will give the following output

Maximum value of Long is :9223372036854775807
Double 1.033720368547758E19 long value=9223372036854775807
Double 65.3 long value=65
Double 65.0 long value=65

Suggested Reading List :

References :