Create Short object
In this chapter you will learn:
Create Short object with its constructors
We can create Short type objects in two ways:
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) {
/*from j a v a 2s . c o m*/
Short shortObject = new Short("1000");
System.out.println(shortObject);
short s = 101;
Short shortObject2 = new Short(s);
System.out.println(shortObject2);
}
}
The output:
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);//j a v a 2 s.com
}
}
Next chapter...
What you will learn in the next chapter:
Home » Java Tutorial » Primitive Data Types