java.lang.Double intValue()

Syntax:

public int intValue()

Java Code Example :

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

Some takeaways on the intValue method:

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

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

public class DoubleIntValue {

	public static void main(String[] args) {

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

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

		Double anotherValue = 37.3;
		int anotherResult = anotherValue.intValue();
		System.out.println("Double " + anotherValue + " int value="
				+ anotherResult);

		Double testValue = 37.0;
		int testResult = testValue.intValue();
		System.out.println("Double " + testValue + " int value=" 
				+ testResult);

	}

}

Sample Output :

Running the above example source code will give the following output

Maximum value of Integer is :2147483647
Double 5.1474836491321E9 int value=2147483647
Double 37.3 int value=37
Double 37.0 int value=37

Suggested Reading List :

References :