Short.valueOf() : This method is available in java.lang.Short class of Java.
Approach 1: When the method takes one argument of type short.
Syntax:
Short java.lang.Short.valueOf(short s)
This method takes one argument of type short as its parameter. This method returns a Short instance representing the specified short value.
Parameters: One parameter is required for this method.
s: a short value.
Returns: a Short instance representing s.
Exceptions: NA
Java
public class ShortvalueOf {public static void main(String[] args) {short s = 14;System.out.println(Short.valueOf(s));}}
Output:
14
Approach 2: When the method takes one argument of type String.
Syntax:
Short java.lang.Short.valueOf(String s) throws NumberFormatException
This method takes one argument of type String as its parameter. This method returns a Short object holding the value given by the specified String.
Parameters: One parameter is required for this method.
s: the string to be parsed.
Returns: a Short object holding the value represented by the string argument.
Throws:
NumberFormatException - If the String does not contain a parsable short.
Java
public class ShortvalueOf2 {public static void main(String[] args) {String s = "123";System.out.println(Short.valueOf(s));}}
Output:
123
Approach 2.1: NumberFormatException
Java
public class ShortvalueOf2 {public static void main(String[] args) {String s = "hello";System.out.println(Short.valueOf(s));}}
Output:
Exception in thread "main" java.lang.NumberFormatException: For input string: "hello" at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:68) at java.base/java.lang.Integer.parseInt(Integer.java:652) at java.base/java.lang.Short.parseShort(Short.java:130) at java.base/java.lang.Short.valueOf(Short.java:186) at java.base/java.lang.Short.valueOf(Short.java:212)
Approach 3: When the method takes two arguments.
Syntax:
Short java.lang.Short.valueOf(String s, int radix) throws NumberFormatException
This method takes two arguments one of type String and the other of type int as its parameter. This method returns a Short object holding the value extracted from the specified String when parsed with the radix given by the second argument. The first argument is interpreted as representing a signed short in the radix specified by the second argument.
Parameters: Two parameters are required for this method.
s: the string to be parsed.
radix: the radix to be used in interpreting s.
Returns: a Short object holding the value represented by the string argument in the specified radix.
Throws:
NumberFormatException - If the String does not contain a parsable short.
Java
public class ShortvalueOf3 {public static void main(String[] args) {String s = "101";int radix = 2;System.out.println(Short.valueOf(s, radix));}}
Output:
5
Approach 3.1: NumberFormatException
Java
public class ShortvalueOf3 {public static void main(String[] args) {String s = "8891";int radix = 2;System.out.println(Short.valueOf(s, radix));}}
Output:
Exception in thread "main" java.lang.NumberFormatException: For input string: "8891" under radix 2 at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:68) at java.base/java.lang.Integer.parseInt(Integer.java:652) at java.base/java.lang.Short.parseShort(Short.java:130) at java.base/java.lang.Short.valueOf(Short.java:186)
No comments:
Post a Comment