- Java.lang Package classes
- Java.lang - Home
- Java.lang - Boolean
- Java.lang - Byte
- Java.lang - Character
- Java.lang - Character.Subset
- Java.lang - Character.UnicodeBlock
- Java.lang - Class
- Java.lang - ClassLoader
- Java.lang - Compiler
- Java.lang - Double
- Java.lang - Enum
- Java.lang - Float
- Java.lang - InheritableThreadLocal
- Java.lang - Integer
- Java.lang - Long
- Java.lang - Math
- Java.lang - Number
- Java.lang - Object
- Java.lang - Package
- Java.lang - Process
- Java.lang - ProcessBuilder
- Java.lang - Runtime
- Java.lang - RuntimePermission
- Java.lang - SecurityManager
- Java.lang - Short
- Java.lang - StackTraceElement
- Java.lang - StrictMath
- Java.lang - String
- Java.lang - StringBuffer
- Java.lang - StringBuilder
- Java.lang - System
- Java.lang - Thread
- Java.lang - ThreadGroup
- Java.lang - ThreadLocal
- Java.lang - Throwable
- Java.lang - Void
- Java.lang Package extras
- Java.lang - Interfaces
- Java.lang - Errors
- Java.lang - Exceptions
- Java.lang Package Useful Resources
- Java.lang - Useful Resources
- Java.lang - Discussion
Java - Short ValueOf() Method
The Java Short ValueOf() method retrieves a Short object holding the value given by the specified String; or short value; or the specified String when parsed with the radix given by the second argument (depending on the type of value assigned).
This method has three polymorphic variants. They are −
valueOf(short s) − This method retrieves a Short instance representing the specified short value s.
valueOf(string s) − This method retrieves the Short object equivalent to the string value provided.
valueOf(String s, int radix): ) − This method retrieves the Short object which is equal to the value of new Short(Short.parseShort(s, radix))
Syntax
Following is the syntax for Java Short ValueOf() method −
public static Short valueOf(short s) // first syntax or, public static Short valueOf(String s) throws NumberFormatException // second syntax or, public static Short valueOf(String s, int radix) throws NumberFormatException // third syntax
Parameters
s − This is the short value. // first syntax
s − This is the string to be parsed. // second and third syntax
radix − This is the radix to be used in interpreting s // third syntax
Return Value
This method returns −
a Short instance representing s. // first syntax
a Short object holding the value represented by the string argument. // second syntax
a Short object holding the value represented by the string argument in the specified radix. // third argument
Example
In the example given below a short variable 'shortNum' is created and a value is assigned to it. Then it returns the Short instance representing given short value using Java Short valueOf(short s) method of Short class −
import java.lang.*; public class ShortDemo { public static void main(String[] args) { short shortNum = 100; // returns Short instance representing given short value Short ShortValue = Short.valueOf(shortNum); // displays the short object value System.out.println("Short object representing the specified short value = " + ShortValue); } }
Output
Let us compile and run the above program, this will produce the following result −
Short object representing the specified short value = 100
Example
Following is an example of the Java Short ValueOf() method −
import java.lang.*; public class ShortDemo { public static void main(String[] args) { short shortNum = 100; String str = "600"; // returns Short object representing given short value Short ShortValue = Short.valueOf(shortNum); // displays the short object value System.out.println("Short object representing the specified short value = " + ShortValue); // returns a Short object holding the value given by the specified String ShortValue = Short.valueOf(str); // displays the short object value System.out.println("Short object holding the value given by the specified String = " + ShortValue); } }
Output
Following is an output of the above code −
Short object representing the specified short value = 100 Short object holding the value given by the specified String = 600
Example
In the example that follows a Short object holding the value from the specified string according to radix is returned using valueOf(String s, int radix) method −
import java.lang.*; public class ShortDemo { public static void main(String[] args) { short shortNum = 100; String str = "1000"; // returns Short object representing given short value Short ShortValue = Short.valueOf(shortNum); // displays the short object value System.out.println("Short object representing the specified short value = " + ShortValue); // returns a Short object holding the value given by the specified String ShortValue = Short.valueOf(str); // displays the short object value System.out.println("Short object holding the value given by the specified String = " + ShortValue); /* returns a Short object holding the value from the specified String according to radix. */ ShortValue = Short.valueOf(str , 2) ; // displays the short object value System.out.println("Short object value for specified String with radix 2 = " + ShortValue); // returns a Short object holding the value for string to radix ShortValue = Short.valueOf("15" , 8) ; // displays the short object value System.out.println("Short object value String 15 with radix 8 = " + ShortValue); } }
Output
Output of the above code is as follows −
Short object representing the specified short value = 100 Short object holding the value given by the specified String = 1000 Short object value for specified String with radix 2 = 8 Short object value String 15 with radix 8 = 13
Example
In the following example the value assigned to the string variable ‘value3’ does not contain a parsable number and hence it throws exception error −
import java.lang.Double; public class ShortDemo { public static void main(String[] args) { String value1 = "9089"; String value2 = "-2567"; String value3 = "Deepak@21"; // returning the equivalent short object of the string value System.out.println("The Short object Value is = " + Short.valueOf(value1)); System.out.println("The Short object Value is = " + Short.valueOf(value2)); System.out.println("The Short object Value is = " + Short.valueOf(value3)); } }
NumberFormatException
While executing the above code we get the following output −
The Short object Value is = 9089 The Short object Value is = -2567 Exception in thread "main" java.lang.NumberFormatException: For input string: "Deepak@21" at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:67) at java.base/java.lang.Integer.parseInt(Integer.java:668) at java.base/java.lang.Short.parseShort(Short.java:137) at java.base/java.lang.Short.valueOf(Short.java:193) at java.base/java.lang.Short.valueOf(Short.java:219) at ShortDemo.main(ShortDemo.java:11)