Short.decode(): This method is available in java.lang.Short class of Java.
Syntax:
Short java.lang.Short.decode(String nm) throws NumberFormatException
This method takes one argument of type String as its parameter. This method decodes a String into a Short. Accepts decimal, hexadecimal, and octal numbers.
Parameters: One parameter is required for this method.
nm: the String to decode.
Returns: a Short object holding the short value represented by nm.
Throws:
1. NumberFormatException - if the String does not contain a parsable short.
Approach 1: When no exceptions.
Java
public class Shortdecode {public static void main(String[] args) {String nm = "123";System.out.println(Short.decode(nm));}}
Output:
123
Approach 2: NumberFormatException
Java
public class Shortdecode {public static void main(String[] args) {String nm = "hello";System.out.println(Short.decode(nm));}}
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.Integer.valueOf(Integer.java:957) at java.base/java.lang.Integer.decode(Integer.java:1442) at java.base/java.lang.Short.decode(Short.java:319)
No comments:
Post a Comment