Declaration
The declaration of the short class is as follows:
public final class Short extends Number implements Comparable<Short>

You can also try this code with Online Java Compiler
Run Code
Field
The fields for the java short class are defined below:
- Short MAX_VALUE: This is a constant that is holding the maximum value of the short can have, 215 - 1
- Short MIN_VALUE: This is a constant that is holding the minimum value of the short can have, -215
- class Type: This is the class instance representing the primitive type short.
- int SIZE: This is the number of bits used to represent the short value in the 2’s complement binary form.
- int BYTES: This is the number of bits used to represent the short value in the 2’s complement binary form.
Constructor
The Java short class provides mainly two types of constructors, these are as follows:
- Short(short param): It accepts a parameter of type short and constructs a newly allocated Short object that represents the specified short value.
short a = 45;
Short x = new Short(a);

You can also try this code with Online Java Compiler
Run Code
- Short(String s): It accepts a parameter of type String and converts that String into Short class object.
String b = "45";
Short y = new Short(b);

You can also try this code with Online Java Compiler
Run Code
Methods
Some of the most useful Java short class methods are as follows:
byteValue()
The method is described below:
Syntax
byte byteValue()

You can also try this code with Online Java Compiler
Run Code
Return value
It returns the value of the short as a byte.
Example
public class ShortByteValue {
public static void main(String[] args) {
Short a = 10;
byte b = a.byteValue();
System.out.println("Byte b = " + b);
}
}

You can also try this code with Online Java Compiler
Run Code
Output

Try it on java online compiler and Hashcode Method in Java
Compare()
The method is described below:
It basically compares two shorts values numerically.
Syntax
public static int compare(short x , short y)

You can also try this code with Online Java Compiler
Run Code
Return Value
If both the short values are equal then it returns 0.
If the first short value is greater than the second one, it returns a positive integer.
If the first short value is less than the second short value, it returns a negative integer.
Example
public class ShortCompare{
public static void main(String[] args) {
short a = 10;
short b = 10; // a == b
short c = 5; // a > c
short d = 15; // a < d
Short aa = new Short(a);
Short bb = new Short(b);
Short cc = new Short(c);
Short dd = new Short(d);
int result1 = Short.compare(aa , bb);
int result2 = Short.compare(aa , cc);
int result3 = Short.compare(aa , dd);
System.out.println("Compared result for a and b is: " + result1);
System.out.println("Compared result for a and c is: " + result2);
System.out.println("Compared result for a and d is: " + result3);
}
}

You can also try this code with Online Java Compiler
Run Code
Output

compareTo()
The method is described below:
It compares a Short object to another Short object numerically.
Syntax
public int compareTo(Short x)

You can also try this code with Online Java Compiler
Run Code
Return Value
If both the short values are equal then it returns 0.
If the short value is numerically greater than to the argument short, it returns a positive integer.
If the short value is numerically less than the argument short value, it returns a negative integer.
Example
public class ShortCompareTo {
public static void main(String[] args) {
Short s1 = 5;
Short s2 = 5; // s1 == s2
Short s3 = 10; // s1 < s3
Short s4 = 3; // s1 > s4
int res1 = s1.compareTo(s2);
int res2 = s1.compareTo(s3);
int res3 = s1.compareTo(s4);
System.out.println("CompareTo result for s1 and s2: " + res1);
System.out.println("CompareTo result for s1 and s3: " + res2);
System.out.println("CompareTo result for s1 and s4: " + res3);
}
}

You can also try this code with Online Java Compiler
Run Code
Output

compareUnsigned()
The method is discussed below:
It is used to compare the two short values numerically where it treats every short value as unsigned.
Syntax
public static int compareUnsigned(short x, short y)

You can also try this code with Online Java Compiler
Run Code
Return Value
If both the unsigned short values are equal then it returns 1.
If the first unsigned short value is greater than the second one, it returns a positive integer.
If the first unsigned short value is less than the second short value, it returns a negative integer.
Example
public class ShortCompareUnsigned {
public static void main(String[] args) {
Short a = 10;
Short b = 10; //a == b
Short c = 20; // a < c
Short d = 5; // a > d
int res1 = Short.compareUnsigned(a , b);
int res2 = Short.compareUnsigned(a , c);
int res3 = Short.compareUnsigned(a , d);
System.out.println("CompareUnsigend Result for a and b is: " + res1);
System.out.println("CompareUnsigned Result for a and c is: " + res2);
System.out.println("CompareUnsigned Result for a and d is: " + res3);
}
}

You can also try this code with Online Java Compiler
Run Code
Output

decode()
The method is discussed below.
It decodes a String into Short.
Syntax
public static Short decode(String a) throws NumberFormatException

You can also try this code with Online Java Compiler
Run Code
Return Value
It returns a short object that holds short values represented by the string argument.
Example
public class ShortDecode {
public static void main(String[] args) {
String val = "10";
Short result = Short.decode(val);
System.out.println("The decoded string value is: " + result);
}
}

You can also try this code with Online Java Compiler
Run Code
Output

parseShort()
The method is discussed below.
It returns a primitive byte value contained in the string argument str.
Syntax
static short parseShort(String x)throws NumberFormatException

You can also try this code with Online Java Compiler
Run Code
Return Value
This method returns a short value representing the string x.
Example
public class ParseShort {
public static void main(String[] args) {
String x = "10";
Short res = Short.parseShort(x);
System.out.println("The Short value representing the string x is : " + res);
}
}

You can also try this code with Online Java Compiler
Run Code
Output

valueOf()
The method is discussed below.
It basically converts the string s(containing short value) into Short class object and returns that Short object.
Syntax
public static Short valueOf(short s) throws NumberFormatException
public static Short valueOf(String s) throws NumberFormatException
public static Short valueOf(String s, int radix) throws NumberFormatException

You can also try this code with Online Java Compiler
Run Code
Return Value
It returns a short object that holds the value represented by the string, or short values.
Example
public class ValueOf {
public static void main(String[] args) {
Short val1 = 10;
String val2 = "1000";
Short res1 = Short.valueOf(val1);
Short res2 = Short.valueOf(val2);
Short res3 = Short.valueOf(val2 , 2);
System.out.println("The short object representing the short value is: " + res1);
System.out.println("The short object representing the : " + res2);
System.out.println("The value of the short object is: " + res3);
}
}

You can also try this code with Online Java Compiler
Run Code
Output

equals()
The method is discussed below.
It compares the object with the specified object.
Syntax
boolean equals(Object obj)

You can also try this code with Online Java Compiler
Run Code
Return Value
It returns true if both the objects are the same otherwise, it returns false.
Example
public class Equals {
public static void main(String[] args) {
Short a = 5;
Short b = 5; // a == b
Short c = 10; // a != c
boolean res1 = a.equals(b);
boolean res2 = a.equals(c);
System.out.println("Equals result for a and b: " + res1);
System.out.println("Equals result for a and c: " + res2);
}
}

You can also try this code with Online Java Compiler
Run Code
Output

Also see, Duck Number in Java
FAQs
-
What is a short class in Java?
The java short class is a wrapper class used to wrap the value of the primitive short data type to an object data type.
-
What is a wrapper class in Java?
A wrapper class in Java is a class that is used to convert a primitive data type to an object data type.
-
Is String a wrapper class in Java?
No String is not a wrapper class in Java.
-
Is boolean a wrapper class in Java?
Yes, a wrapper class in Java named Boolean wraps the primitive data type boolean.
Key Takeaways
In this article, we have extensively discussed the Java Short class and their implementation.
We started with the basic concept of the wrapper classes, then we moved to the short class, its basic definition, different fields, types of constructors and different methods using examples.
We hope that this blog has helped you enhance your knowledge regarding Java Short and if you would like to learn more, check out our articles on Java Float Class, Java Double Class. Do upvote our blog to help other ninjas grow.
Recommended Readings:
Head over to our practice platform Coding Ninjas Studio to practice top problems, attempt mock tests, read interview experiences, and much more.!
Happy Reading!