java.lang.Double shortValue()

Syntax:

public short shortValue()

Java Code Example :

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

Some takeaways on the byteValue method:

  • override shortValue in class Number
  • Returns the value of this Double as a short after a narrowing primitive conversion.
  • Since JDK 1.1
package com.javatutorialhq.java.examples;

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

public class DoubleShortValue {

	public static void main(String[] args) {

		
		/*
		 * As you can see on the value of result variable
		 * would be negative since the byte primitive
		 * has  a maximum value of 32767 as indicated by 
		 * calling the static method Short.MAX_VALUE 
		 */
		System.out.println("Maximum value of Short is :"+Short.MAX_VALUE);
		Double val = 32770.1321;
		short result = val.shortValue();
		System.out.println("Double "+val + " short value="+result);
		
		
		/* 
		 * both the variable anotherResult and testResult
		 * will yield to the same value since the short primitive
		 * doesnt have any decimal places, thus the decimal
		 * values will be truncated 
		 */
		
		Double anotherValue = 32.253;
		short anotherResult = anotherValue.shortValue();
		System.out.println("Double "+anotherValue + " short value="+anotherResult);		
		
		Double testValue = 32.0;
		short testResult = testValue.shortValue();
		System.out.println("Double "+testValue + " short value="+testResult);	
		
		
		
		
	}

}

Sample Output :

Running the above example source code will give the following output

Maximum value of Short is :32767
Double 32770.1321 short value=-32766
Double 32.253 short value=32
Double 32.0 short value=32

Suggested Reading List :

References :