English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Convert Java String to Short in Java

UsingvalueOf()Method converts Java String to Short.

Let's go through it.

Here is an example-

String myStr = "5";

Now, using the Short object and usingvalueOf()Method. The parameter should be the string we declared above.

Here is an example.

Short myShort = Short.valueOf(myStr);

Now let's look at the entire example to learn how to convert a string in Java to Short.

Example

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);
   }
}

Output Result

String: 5
Short: 5