Skip to Content

Convert String to short primitive Example

  1. /*
  2.   Convert String to short primitive Example
  3.   This example shows how a String object can be converted into short primitive type
  4.   using parseShort method of Short class.
  5. */
  6.  
  7. public class StringToShortPrimitiveExample {
  8.  
  9.   public static void main(String[] args) {
  10.     //declare String object
  11.     String str = new String("10");
  12.    
  13.     /*
  14.     use parseShort method of Short class to convert String into short primitive
  15.     data type. This is a static method.
  16.     Please note that this method can throw a NumberFormatException if the string
  17.     is not parsable to short.
  18.     */
  19.     short s = Short.parseShort(str);
  20.     System.out.println(s);
  21.   }
  22. }
  23.  
  24. /*
  25. Output the program would be
  26. 10
  27. */

Advertisement: