How to Convert a String value to Short value in Java with Examples Last Updated : 29 Jan, 2020 Summarize Comments Improve Suggest changes Share Like Article Like Report Given a String "str" in Java, the task is to convert this string to short type. Examples: Input: str = "1" Output: 1 Input: str = "3" Output: 3 Approach 1: (Naive Method) One method is to traverse the string and add the numbers one by one to the short type. This method is not an efficient approach. Approach 2: (Using Short.parseShort() method) The simplest way to do so is using parseShort() method of Short class in java.lang package. This method takes the string to be parsed and returns the short type from it. If not convertible, this method throws error. Syntax: Short.parseShort(str); Below is the implementation of the above approach: Example 1: To show successful conversion Java // Java Program to convert string to short class GFG { // Function to convert String to Short public static short convertStringToShort(String str) { // Convert string to short // using parseShort() method return Short.parseShort(str); } // Driver code public static void main(String[] args) { // The string value String stringValue = "1"; // The expected short value short shortValue; // Convert string to short shortValue = convertStringToShort(stringValue); // Print the expected short value System.out.println( stringValue + " after converting into short = " + shortValue); } } Output: 1 after converting into short = 1 Approach 3: (Using Short.valueOf() method) The valueOf() method of Short class converts data from its internal form into human-readable form. Syntax: Short.valueOf(str); Below is the implementation of the above approach: Example 1: To show successful conversion Java // Java Program to convert string to short class GFG { // Function to convert String to Short public static short convertStringToShort(String str) { // Convert string to short // using valueOf() method return Short.valueOf(str); } // Driver code public static void main(String[] args) { // The string value String stringValue = "1"; // The expected short value short shortValue; // Convert string to short shortValue = convertStringToShort(stringValue); // Print the expected short value System.out.println( stringValue + " after converting into short = " + shortValue); } } Output: 1 after converting into short = 1 Comment More infoAdvertise with us Next Article How to Convert a String value to Short value in Java with Examples C code_r Follow Improve Article Tags : Java Java Programs Java-Strings Java-Data Types Java-Short Java-String-Programs +2 More Practice Tags : JavaJava-Strings Similar Reads How to Convert a Short value to String value in Java with Examples Given a Short value in Java, the task is to convert this short value to string type. Examples: Input: 1 Output: "1" Input: 3 Output: "3" Approach 1: (Using + operator) One method is to create a string variable and then append the short value to the string variable. This will directly convert the sho 2 min read How to Convert a String value to Byte value in Java with Examples Given a String "str" in Java, the task is to convert this string to byte type. Examples: Input: str = "1" Output: 1 Input: str = "3" Output: 3 Approach 1: (Naive Method) One method is to traverse the string and add the numbers one by one to the byte type. This method is not an efficient approach. Ap 2 min read How to Convert a Double value to String value in Java with Examples Given a Double value in Java, the task is to convert this double value to string type. Examples: Input: 1.0 Output: "1.0" Input: 3.14 Output: "3.14" Approach 1: (Using + operator) One method is to create a string variable and then append the double value to the string variable. This will directly co 2 min read Java Program to Convert a Float value to String Given a Float value in Java, the task is to write a Java program to convert this float value to string type. Examples: Input: 1.0 Output: "1.0" Input: 3.14 Output: "3.14" Strings - Strings in Java are objects that are supported internally by a char array. Since arrays are immutable, and strings are 4 min read How to Convert a String to an Long in Java ? In Java, String to Integer Conversion is not always effective as the size of the String can overpass Integer. In this article, we will learn the method provided by the Long class or by using the constructor of the Long class. Example of String to Long ConversionInput: " 9876543210"Output: 9876543210 2 min read Like