Friday, October 10, 2025
HomeLanguagesJavaHow to Convert a String value to Short value in Java with...

How to Convert a String value to Short value in Java with Examples

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 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 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
Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32349 POSTS0 COMMENTS
Milvus
87 POSTS0 COMMENTS
Nango Kala
6715 POSTS0 COMMENTS
Nicole Veronica
11878 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11941 POSTS0 COMMENTS
Shaida Kate Naidoo
6837 POSTS0 COMMENTS
Ted Musemwa
7097 POSTS0 COMMENTS
Thapelo Manthata
6792 POSTS0 COMMENTS
Umr Jansen
6791 POSTS0 COMMENTS