Create Short object with its constructor

Short(short value)
Creates a newly allocated Short object that represents the specified short value.
Short(String s)
Creates a newly allocated Short object that represents the short value indicated by the String parameter.

public class Main {
  public static void main(String[] args) {

    Short shortObject = new Short("1000");
    System.out.println(shortObject);

    short s = 101;
    Short shortObject2 = new Short(s);
    System.out.println(shortObject2);

  }
}

The output:


1000
101

You cannot pass integer literal to the constructor directly as Java think it is an int not a short value.


public class Main {
  public static void main(String[] args) {
    Short shortObject2 = new Short(101);
    System.out.println(shortObject2);

  }
}

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
    The constructor Short(int) is undefined

    at Main.main(Main.java:3)
Home 
  Java Book 
    Essential Classes  

Short:
  1. Short class
  2. Find out the min value, max value and size of Short types
  3. Create Short object with its constructor
  4. Convert Short to byte, double, float, int, long and short
  5. Decode a string to short value
  6. Convert string to a short value
  7. Reverse the bytes in a short
  8. Convert short value to string
  9. Compare two short values