Convert Java String to Short in Java

Java 8Object Oriented ProgrammingProgramming

Use valueOf() method to convert a String in Java to Short.

Let us take a string.

The following is an example −

String myStr = "5";

Now take Short object and use the valueOf() method. The argument should be the string we declared above.

The following is an example.

Short myShort = Short.valueOf(myStr);

Let us now see the entire example to learn how to convert a string in Java to Short.


 Live Demo

public class Demo {
   public static void main(String []args) {
      String myStr = "5";
      System.out.println("String: "+myStr);
      Short myShort = Short.valueOf(myStr);
      System.out.println("Short: "+myShort);


String: 5
Short: 5
Updated on 26-Jun-2020 10:23:09