Skip to Content

  1. /*
  2.   Java Short Example
  3.   This example shows how an object of Short can be declared and used.
  4.   Short is a wrapper class provided to wrap short primitive value. It has a single
  5.   field of type short.
  6. */
  7.  
  8. public class ShortExample {
  9.  
  10.   public static void main(String[] args) {
  11.     //create a Short object using one of the below given constructors
  12.     //1. Create a Short object from short
  13.     short s = 10;
  14.     Short sObj1 = new Short(s);
  15.    
  16.     /*
  17.     2. Create Short object from String. Please note that this method can
  18.     throw NumberFormatException if string doesnt contain parsable number.
  19.     */
  20.     Short sObj2 = new Short("10");
  21.    
  22.     //print value of Short objects
  23.     System.out.println(sObj1);
  24.     System.out.println(sObj2);
  25.   }
  26. }
  27.  
  28.  
  29. /*
  30. Output of the program would be
  31. 10
  32. 10
  33. */