Short class in Java is a wrapper class that wraps (converts) a value of primitive data type “short” in an object.
In simple words, a Short is a wrapper around a short.
An object of Short class contains a single field of type short. In this field, we can store a short number.
Short class was introduced in JDK 1.1. It is present in java.lang.Short package.
Short class declaration
The general declaration of Short class in Java is as follows:
public final class Short extends Number implements Comparable<Short>
Short Field Constants
Java Short class provides several field constants that are as follows:
1. static int BYTES: It is the number of bytes that is used to represent a short value in two’s complement binary form. It was introduced in JDK 1.8.
2. static short MAX_VALUE: It represents a constant that can hold the maximum value of short (215 – 1).
3. static short MIN_VALUE: It represents a constant that can hold the minimum value of short ( -215 ).
4. static int SIZE: It is the number of bits that is used to represent a short value in two’s complement binary form. It was introduced in JDK 1.5.
5. static Class<Short> TYPE: It is a Class instance that represents the primitive type short.
Java Short Class Constructor
Short class in java provides two constructors that are as follows:
1. Short(short num): This form of constructor accepts short number as its parameter and converts it into Short class object. The general syntax to create Short class object is as follows:
Short obj = new Short(14000);
2. Short(String str): This constructor accepts a parameter of String type and converts that string into Short class object. The general syntax to create a Short object by wrapping a string that contains a short number, as follows:
String str = "14000"; Short obj = new Short(str);
Important Short Class Methods in Java
In addition to methods inherited from Number class and Object class, Java Short class also provides some useful important methods. They are as follows:
1. int compareTo(Short obj): This method is used to compare contents of two Short class objects numerically. It is called using the below syntax.
int x = obj1.compareTo(obj2);
where, obj1 and obj2 are Byte class objects.
- If obj1 == obj2, this method returns 0.
- If obj1 < obj2, it returns negative value.
- If obj1 > obj2, it returns positive value.
2. boolean equals(Object obj): This method is used to compare Short object with another Short object obj. If both have the same contents, this method returns true otherwise returns false.
3. static short parseShort(String str): This method returns the primitive byte value contained in the string argument str.
4. String toString(): The toString() method converts Short object into String object and returns that String object. It returns a string form of Short object.
5. static Short valueOf(String str): It converts string str containing some short value into Short class object and returns that Short object.
In other words, it returns a Short object holding a value given by the specified String.
6. static Short valueOf(short s): This method converts the primitive short value into Short class object. In other words, it returns a Short object representing the specified short value.
7. static int compare(short x, short y): It is used to compare two short values numerically. It returns 0, -ve value, or +value.
8. static Short decode(String str): This method decodes a String into a Short.
9. static int toUnsignedInt(short x): This method converts short value to an int by an unsigned conversion.
10. static long toUnsignedLong(short x): This method converts short value to a long by an unsigned conversion.
Hope that this tutorial has covered important topics related to Java Short class with example program. I hope that you will have understood this simple topic.
Thanks for reading!!!
Next ⇒ Integer Class in Java