The Short class is simply a wrapper class for the primitive type short. It wraps the short primitive value to an object. An object of type Short contains a single field whose type is byte. So what would be the benefit if wraps around the primitive data type to an object? Well basically we would be able to access all the methods that is readily available on the Short class which is very useful. In addition, this class provides a decent amount of methods for converting a short to a String and vice versa, as well as other constants and methods useful when dealing with a short. We will go over each and every method available to Short class thoroughly.

Short Class Syntax

public final class Short
extends Number
implements Comparable<Short>

Short Compatibility Version

Byte Class is available since JDK 1.1

Short Basic Usage

The Short class as part of the java.lang package is one of the classes of the java api that is being widely used. Let’s tackle the basics of using the Short class by discussing how to instantiate a Short object

Basically the declaration of a Short object is almost the same as we are doing in assigning primitive data type short. Similarly to short primitive the Short object has only two possible values, which is either true or false. The above example is a straightforward method to instantiate a Short object type. Let’s take another way to do this.

The 2nd method is using the autoboxing concept of java programming language which directly converts a primitive data type to its corresponding wrapper class.  If you would like to go more into details, I am recommending for you to read the official java documentation regarding autoboxing and unboxing.

There are two constructor available to Short class which we can use to instantiate a Short object:

Constructor Description
Short​(short value) Deprecated as of JDK 11
It is rarely appropriate to use this constructor.
Short​(String s) Deprecated.
It is rarely appropriate to use this constructor.

We already discussed on how to use the Short(short value) in instantiating a Short object. So basically the other constructors usage is almost the same only in the other case, the input is in String format.

Short Test of Equality

Normally when we do equality test for Byte primitive data type we do like this:

The above example will no longer work in dealing with Short object type. To test equality we need to make use of equals which is a method inherited from Object class.

If the condition is to test equality between 1 primitive type short and 1 Object Short, then you need either to normalize both to primitive by using the method shortvalue() and then use the test ==, or normalize both to Short(using autoboxing) and then test equality by using the method equals().

Byte Method Usage Examples

Modifier and Type Method and Description