Create Short object with its constructor

ConstructorSummary
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)
java2s.com  |  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.