Convert Java String to Short in Java

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

karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know

Updated on: 26-Jun-2020

5K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started