java.lang.Short

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

Short value = new Short(12);

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.

Short value = 12;

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:

		Short b1= 12;
		short b2 = 12;
		if(b1==b2){
			System.out.println("They are equal");
		}
		else{
			System.out.println("Values are not equal");
		}

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.

		Short val1 = 12;
		Short val2 = new Short(12);
		if(val1.equals(val2)){
			System.out.println("They are equal");
		}
		else{
			System.out.println("They are not equal");			
		}

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
byte byteValue()
Returns the value of this Short as a byte after a narrowing primitive conversion.
static int compare​(short x, short y)
Compares two short values numerically.
int compareTo​(Short anotherShort)
Compares two Short objects numerically.
static int compareUnsigned​(short x, short y)
Compares two short values numerically treating the values as unsigned.
static Short decode​(String nm)
Decodes a String into a Short.
double doubleValue()
Returns the value of this Short as a double after a widening primitive conversion.
boolean equals​(Object obj)
Compares this object to the specified object.
float floatValue()
Returns the value of this Short as a float after a widening primitive conversion.
int hashCode()
Returns a hash code for this Short; equal to the result of invoking intValue().
static int hashCode​(short value)
Returns a hash code for a short value; compatible with Short.hashCode().
int intValue()
Returns the value of this Short as an int after a widening primitive conversion.
long longValue()
Returns the value of this Short as a long after a widening primitive conversion.
static short parseShort​(String s)
Parses the string argument as a signed decimal short.
static short parseShort​(String s, int radix)
Parses the string argument as a signed short in the radix specified by the second argument.
static short reverseBytes​(short i)
Returns the value obtained by reversing the order of the bytes in the two’s complement representation of the specified short value.
short shortValue()
Returns the value of this Short as a short.
String toString()
Returns a String object representing this Short’s value.
static String toString​(short s)
Returns a new String object representing the specified short.
static int toUnsignedInt​(short x)
Converts the argument to an int by an unsigned conversion.
static long toUnsignedLong​(short x)
Converts the argument to a long by an unsigned conversion.
static Short valueOf​(short s)
Returns a Short instance representing the specified short value.
static Short valueOf​(String s)
Returns a Short object holding the value given by the specified String.
static Short valueOf​(String s, int radix)
Returns a Short object holding the value extracted from the specified String when parsed with the radix given by the second argument.