
- 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 parseShort() Method
The Java Short parseShort() method is used to parse (convert) the given string value into a Short object. The string given is parsed by this method into a signed decimal short.
The characters in the provided the string must all be decimal digits, with the exception of the first character, which can be either an ASCII minus sign ('-') or an ASCII plus sign ('+') ('u002B') to denote a negative or a positive value respectively.
Syntax
Following is the syntax for Java Short parseShort() method −
public static short parseShort(String s) throws NumberFormatException or, public static short parseShort(String s, int radix) throwsNumberFormatException
Parameters
s − This is a String containing the short representation to be parsed.
radix − This is the radix to be used while parsing s.
Return Value
This method returns the short value represented by the string argument in the specified radix.
Example
We can pass a positive value using parseShort() method. The result will be the equivalent short value of the given string value.
The following example shows the usage of Java Short parseShort() method. Here we are by creating a Short object ‘s’ and returning the short value represented by the string variable 'str'.
import java.lang.*; public class ShortDemo { public static void main(String[] args) { Short s = new Short("6"); // returns the short value represented by the string argument String str = "50"; short retval = s.parseShort(str); System.out.println("Value = " + retval); } }
Output
Let us compile and run the above program, this will produce the following result −
Value = 50
Example
In the example given below three string variables i.e. ‘s1’, ‘s2’, ‘s3’ have been declared with values assigned to it. By using the parseShort() method the short values with few operations such as concatenation, addition, multiplication and division corresponding to the strings are passed. Then their corresponding short values are returned −
public class ShortDemo { public static void main(String[] args) { String s1 = "8"; String s2 = "13"; String s3 = "4"; // concatenating two strings instead of adding it Short d1 = Short.parseShort(s1 + s2 + s3); System.out.println("The sum is =" + d1); System.out.println("The sum is =" + (Short.parseShort(s1) + Short.parseShort(s2) + Short.parseShort(s3))); System.out.println("The Product is =" + (Short.parseShort(s1) * Short.parseShort(s2) * Short.parseShort(s3))); System.out.println("The division is =" + (Short.parseShort(s2) / Short.parseShort(s3))); System.out.println("The division =" + (Short.parseShort(s1) / Short.parseShort(s2))); } }
Output
Following is an output of the above code −
The sum is =8134 The sum is =25 The Product is =416 The division is =3 The division =0
Example
When an alpha-numeric values is passed as an argument to this method, it returns a Number Format Exception.
In the code below the string value passed does not contain a parsable value and hence it throws an exception error −
import java.util.Scanner; public class ShortDemo { public static void main(String[] args) { String s = "Deepak@21"; Short d = Short.parseShort(s); System.out.println("The equivalent short value is = " + d); } }
NumberFormatException
The above code throws exception error as shown in the output below −
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.parseShort(Short.java:163) at ShortDemo.main(ShortDemo.java:6)
Example
Following is another example where the signed decimal short value for specified String with radix is retrieved minus;
import java.lang.*; public class ShortDemo { public static void main(String[] args) { String str = "1000"; // returns signed decimal short value of string short shortValue = Short.parseShort(str); // prints signed decimalshort value System.out.println("Signed decimal short value for given String is = " + shortValue); // returns the string argument as a signed short in the radix shortValue = Short.parseShort(str,2); System.out.println("Signed decimal short value for specified String with radix 2 is = " + shortValue); // returns the string argument as a signed short in the radix shortValue = Short.parseShort("11",8); System.out.println("Signed decimal short value for specified String with radix 8 is = " + shortValue); } }
Output
While executing the above code, we get the following output −
Signed decimal short value for given String is = 1000 Signed decimal short value for specified String with radix 2 is = 8 Signed decimal short value for specified String with radix 8 is = 9