• Short is a built-in class in Java available in java.lang package.
• It is a Wrapper class for short primitive data types.
• It is a final type of class, so no other class can inherit java.lang.Short class and we cannot override its methods.
• Objects of java.lang.Short class are always immutable.
This means that when any operation like add, sub, etc. are performed with the existing objects of java.lang.Short class then always new objects get created that store the results.
class A{
public static void main(String arg[]){
Short s1 = new Short("10");
Short s2 = new Short("20");
Short s3 = (short)(s1 + s2);
System.out.println("s1 == s3 :- "+(s1 == s3)); // false
System.out.println("s2 == s3 :- "+(s2 == s3)); // false
}
}
s1 == s3 :- false s2 == s3 :- false
In the above example we can see that we have two different objects of Short class s1 and s2 and in the next line we are adding s1 and s2 and assigning their result in Short s3. Finally we are comparing s1 and s3, s2 and s3 and their corresponding results are false and false respectively. This proves that no changes happened in s1 and s2 which are the existing objects of java.lang.Short class.
public final class java.lang.Short extends java.lang.Number implements java.lang.Comparable<java.lang.Short> {
public static final short MIN_VALUE;
public static final short MAX_VALUE;
public static final java.lang.Class<java.lang.Short> TYPE;
public static final int SIZE;
public static final int BYTES;
public static java.lang.String toString(short);
public static short parseShort(java.lang.String, int) throws java.lang.NumberFormatException;
public static short parseShort(java.lang.String) throws java.lang.NumberFormatException;
public static java.lang.Short valueOf(java.lang.String, int) throws java.lang.NumberFormatException;
public static java.lang.Short valueOf(java.lang.String) throws java.lang.NumberFormatException;
public static java.lang.Short valueOf(short);
public static java.lang.Short decode(java.lang.String) throws java.lang.NumberFormatException;
public java.lang.Short(short);
public java.lang.Short(java.lang.String) throws java.lang.NumberFormatException;
public byte byteValue();
public short shortValue();
public int intValue();
public long longValue();
public float floatValue();
public double doubleValue();
public java.lang.String toString();
public int hashCode();
public static int hashCode(short);
public boolean equals(java.lang.Object);
public int compareTo(java.lang.Short);
public static int compare(short, short);
public static short reverseBytes(short);
public static int toUnsignedInt(short);
public static long toUnsignedLong(short);
public int compareTo(java.lang.Object);
static {};
}
As we can see that in the above API of java.lang.Short class, we have different utility methods or variables to perform different operations with byte type of values.
Here we will discuss about some important members of java.lang.Short class: -
Access Modifier: - public
Member Type: - static final variable
Data Type: - short
variable Name: - MIN_VALUE
Functionality: - The variable is used for storing the minimum value (-32768) which can be stored in the short type of variable and the same minimum value can be passed as an argument into the java.lang.Short class constructor for creating an object.
When we try to store a value which is lesser than the minimum value (-32768) then we get Incompatible type error at compile-time.While trying to create an object of java.lang.Short class, we will get a runtime exception i.e. java.lang.NumberFormatException, if we pass into the constructor an argument of value lesser than the lower limit.
class JTC{
public static void main(String arg[]){
System.out.println("Short.MIN_VALUE :- "+Short.MIN_VALUE);
short s1 = -32768;
// short s2 = -32769; --> incompatible types
Short s3 = new Short("-32768");
//Short s4 = new Short("-32769"); --> java.lang.NumberFormatException
}
}
Short.MIN_VALUE :- -32768
In the above example we can see that in JTC class, first we have declared a byte type of variable which is short s1 and have assigned it a value of -32768. Since -32768 is in the range of short primitive data type values, so it is valid.
In the next line we have variable short s2 = -32769 and as we have discussed that -32769 is not in the range of short primitive data type values, so for this value we get Incompatible error at compile time.
Then after we are creating an object of java.lang.Short class (Short b3 = new Short (“-32768”)) and as an argument we are passing a value of -32768 into java.lang.Short class constructor. Since this value is in the range of values that can be entered as string in Short, so it is valid.
Finally in the last section of the above example we are creating an object of java.lang.Short class with -32769 as an argument. Since -32769 is not in the range of Short class values, so although we will not get any error at compile-time, we get java.lang.NumberFormatException at runtime that shows that the statement is invalid.
Access Modifier: - public
Member Type: - static final variable
Data Type: - short
Variable Name: - MAX_VALUE
Functionality: - It stores the maximum value (32767) which we can store into the short type of variable and the same maximum value we can pass as an argument into the java.lang.Short class constructor for creating an object.
When we try to store a value which is greater than the maximum value (32767) then we get Incompatible type error at compile-time. For creating an object of java.lang.Short class if we pass into a constructor an argument of value greater than the upper limit then we get java.lang.NumberFormatException exception at runtime.
class JTC{
public static void main(String arg[]){
System.out.println("Short.MAX_VALUE :- "+Short.MAX_VALUE);
short s1 = 32767;
// short s2 = 32768; //--> incompatible types
Short s3 = new Short("32767");
// Short s4 = new Short("32768"); //--> java.lang.NumberFormatException
}
}
Short.MAX_VALUE :- 32767
In the above example we can see that in JTC class first we have declared a short type of variable which is short s1 and thereafter have assigned it a value of 32767. Since 32767 is in the range of short primitive data type values, so it is valid.
In the next line we have one more variable short s2 = 32768 and as we have discussed that 32768 is not in the range of byte values, so here we get Incompatible error at compile time.
Then after we are creating an object of java.lang.Short class (Short s3 = new Short (“32767”)) and we are passing into java.lang.Short class constructor an argument of value 32767. Since this value is in the range of Short class String values, so it is valid.
Finally in the last section of the above example we are creating an object of java.lang.Short class along with 32768 as argument Since 32768 is not in the range of Short values, so although we do not get any compile time error, we get java.lang.NumberFormatException at runtime that shows that the statement is invalid.
Access Modifier: - public
Member Type: - static Method
Return Type: - short
Method Name: - parseShort
Parameter: - java.lang.String
Functionality: - As we know that we cannot type-cast a String type value into the short type, but sometimes as per our business logic we convert the String literals into short type using parseShort() method.
While using parseShort(), we need to commit two points into our memory - firstly the argument of parseShort(String) must contain numeric value and secondly this numeric value must be in the range of short which is -32768 to 32767, otherwise we get an exception i.e., java.lang.NumberFormatException.
class JTC{
public static void main(String arg[]){
short s1 = Short.parseShort("32767");
System.out.println("s1 :- "+s1);
// short s2 = Short.parseShort("32768"); --> java.lang.NumberFormatException
// short s3 = Short.parseShort("abc"); --> java.lang.NumberFormatException
}
}
s1 :- 32767
In the above example as we can see that in the first invocation of parseShort() method we are passing “32767” as argument .The argument is in number format and its value 32767 is in the range of Short values, so this statement is valid.
In the next two invocations of parseShort() method we are passing “32768” and “abc” as String value arguments respectively. As we have discussed that “32768” is not in the range of Short and “abc” is not in number format, so at the time of program execution we get java.lang.NumberFromatException for both the invocations.
Access Modifier: - public
Member Type: - Instance Method
Return Type: - int
Method Name: - hashCode()
Parameter: - No parameter
Functionality: - hashCode() method is overridden from java.lang.Object class to java.lang.Short class. In java.lang.Short class the method returns same value which we passed as argument into Short class constructor for creating current working object of java.lang.Short class.
class JTC{
public static void main(String arg[]){
Short s1 = new Short("32767");
System.out.println("s1 hashcode :- "+s1.hashCode());
}
}
s1 hashcode :- 32767
In the above example we are creating an object of java.lang.Short class and passing “32767” as an argument (Short s1 = new Short (“32767”)) and in the next line we are invoking hashCode() along with s1. As we discussed earlier, hashCode() method is overridden in java.lang.Short class and that is the reason we are getting 10 as the output here.
Access Modifier: - public
Member Type: - Instance Method
Return Type: - boolean
Method Name: - equals()
Parameter:- Object
Functionality: - equals() method is overridden in java.lang.Short class, and here the method compares given object to the specified object. The result is true if and only if the argument is not null and is a Short object that contains the same short value as the specified object.
class A{
public static void main(String arg[]){
Short s1 = new Short("10");
Short s2 = new Short("20");
System.out.println("s1.equals(s2) :- "+(s1.equals(s2))); // false
Short s3 = new Short("10");
System.out.println("s1.equlas(s3) :- "+(s1.equals(s3))); // true
}
}
s1.equals(s2) :- false s1.equlas(s3) :- true
In this example we are creating two different objects of Short class- Short s1 = new Short (“10”) and Short s2 = new Short (“20”) and we are getting false because we are passing two different values to create objects of java.lang.Short class. In the next line again, we are creating an object of Short class (Short s3 = new Short (“10”)) and then after we are comparing s1 and s3 using equals () method; now because both objects are containing the same value, we will get true as the result.
Access Modifier: - public
Member Type: - Instance Method
Return Type: - int
Method Name: - compareTo()
Parameter:- java.lang.Short
Functionality: - The method returns 0 if the given Short is equal to the argument Short; a value less than 0 if the Short is numerically less than the argument Short; and a value greater than 0 if the Short is numerically greater than the argument Short.
class JTC{
public static void main(String arg[]){
Short s1 = new Short ("10");
Short s2 = new Short ("10");
System.out.println("s1.compareTo(s2) :- "+(s1.compareTo(s2))); // 0
Short s3 = new Short ("11");
System.out.println("s1.compareTo(s3) :- "+(s1.compareTo(s3))); // -1
Short s4 = new Short ("9");
System.out.println("s1.compareTo(s4) :- "+(s1.compareTo(s4))); // 1
}
}
s1.compareTo(s2) :- 0 s1.compareTo(s3) :- -1 s1.compareTo(s4) :- 1
In the above example we are creating two different objects of java.lang.Short with same value which is 10. That is the reason when we compare s1 and s2 we get the return as 0.
In the next line we are creating an object of java.lang.Short with value 11 (Short s3 = new Short (“11”)) and as we can see s1 has short value of 10, and that is the reason when we compare s1 and s3 we get -1.
Finally, in the last line we are creating an object of Short which is Short s4 = new Short (“9”), that is the reason we are getting 1 as a result of s1.compareTo(s4).