Java.lang.Number.shortValue() Method

Advertisements


Description

The java.lang.Number.shortValue() method returns the value of the specified number as a short. This may involve rounding or truncation.

Declaration

Following is the declaration for java.lang.Number.shortValue() method

public abstract short shortValue()

Parameters

  • NA

Return Value

This method returns the numeric value represented by this object after conversion to type short.

Exception

  • NA

Example

The following example shows the usage of lang.Number.shortValue() method.

package com.tutorialspoint;

public class NumberDemo {

   public static void main(String[] args) {

      // get a number as float
      Float x = new Float(123456f);

      // get a number as double
      Double y = new Double(9876);

      // print their value as short
      System.out.println("x as float :" + x
              + ", x as short:" + x.shortValue());
      System.out.println("y as double:" + y
              + ", y as short:" + y.shortValue());

   }
}

Let us compile and run the above program, this will produce the following result:

x as float :123456.0, x as short:-7616
y as double:9876.0, y as short:9876



Advertisements
Advertisements