java.lang.Integer shortValue()

Description :

This java tutorial shows how to use the shortValue() method of Integer class under java.lang package. This method returns the short equivalent of the Integer object. This method override the shortValue() method of Number class.

Method Syntax :

public short shortValue()

Parameter Input :

DataType Parameter Description
N/A N/A N/A

Method Returns :

The shortValue() method returns the numeric value represented by this object after conversion to type short.

Compatibility Version :

Requires Java 1.0 and up

Exception :

N/A

Java Code Example :

This java example source code demonstrates the use of shortValue() method of Integer class. Basically we ask for user input on the console and then we use the scanner object to get the int input. After that we assign the value to an Integer wrapper class and then get the short equivalent using the shortValue() method of Integer class.

package com.javatutorialhq.java.examples;

import java.util.Scanner;

import static java.lang.System.*;

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

public class IntegerShortValue {

	public static void main(String[] args) {
		// Ask user input
		System.out.print("Enter Desired Value:");
		// declare the scanner object
		Scanner scan = new Scanner(System.in);
		// use scanner to get the value from user console
		int intValue = scan.nextInt();
		// close the scanner object
		scan.close();
		Integer myValue = new Integer(intValue);
		out.println("short Value is " + myValue.shortValue());

	}

}

Sample Output :

Running the toOctalString() method example source code of Integer class will give you the following output

Enter Desired Value:654
short Value is 654

Exception Scenario :

N/A

Similar Method :

  • N/A

Suggested Reading List :

References :