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.

Example

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

Output

String: 5
Short: 5
raja
Published on 12-Nov-2018 12:28:58
Advertisements