Integer shortValue() Method in Java
The Integer.shortValue() is an inbuilt method of java.lang which returns the value of this Integer in the short type .
Syntax:
public short shortValue()
Parameters: The method does not take any parameters.
Return Value: The method returns the integer value represented by this object after converting it to type short.
Below programs illustrate the Integer.shortValue() method:
Program 1: For positive integers.
// Java program that demonstrates // Integer.shortValue() method import java.lang.*; public class Geeks { public static void main(String[] args) { Integer sh_object = new Integer(763); // It will return the value of this Integer as a short type short sh_value = sh_object.shortValue(); System.out.println(" The Value of sh_value = " + sh_value); } } |
The Value of sh_value = 763
Program 2: For negative number.
// Java program that demonstrates // Integer.shortValue() method import java.lang.*; public class Geeks { public static void main(String[] args) { Integer sh_object = new Integer(-43); // It will return the value of this Integer as a short type short sh_value = sh_object.shortValue(); System.out.println(" The Value of sh_value = " + sh_value); } } |
The Value of sh_value = -43
Program 3: For a decimal value and string.
Note: It returns an error message when a decimal value and string is passed as an argument.
// java program that demonstrates // Integer.shortValue() method import java.lang.*; public class Geeks { public static void main(String[] args) { // passing a decimal value Integer sh_object = new Integer(27.51); short sh_value = sh_object.shortValue(); System.out.println(" The Value of sh_value = " + sh_value); // passing a string Integer sh_object2 = new Integer("51"); short sh_value2 = sh_object2.shortValue(); System.out.println(" The Value of sh_value2 = " + sh_value2); } } |
Output:
prog.java:10: error: no suitable constructor found for Integer(double)
Integer sh_object = new Integer(27.51);
^
constructor Integer.Integer(int) is not applicable
(argument mismatch; possible lossy conversion from double to int)
constructor Integer.Integer(String) is not applicable
(argument mismatch; double cannot be converted to String)
1 error
Recommended Posts:
- Short shortValue() Method in Java
- Double shortValue() method in Java with examples
- Byte shortValue() method in Java with examples
- Number.shortValue() method in java with examples
- Float shortValue() method in Java with examples
- Integer sum() Method in Java
- Integer.numberOfLeadingZeros() Method in Java With Example
- Integer reverseBytes() Method in Java
- Integer.numberOfTrailingZeros() Method in Java with Example
- Integer rotateRight() Method in Java
- Java Integer compareTo() method
- Java Integer compareUnsigned() method
- Integer signum() Method in Java
- Java Integer compare() method
- Java Integer byteValue() method
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




