Java Short Class
Short class is a wrapper class that is use to wrap a value of primitive short type in an object type.
An object of type Short contains a single field whose type is short.
This class provides several methods for converting a short to a String and a String to a short, as well as other constants and methods helpful while working with a short type. Declaration of short type is given below.
Declaration:
Here are the Methods of Short class and their example.
1. toString()
This method is used to get string representation of byte object. It returns a new String object representing the specified short. It takes String type single argument.
Syntax:
Example:
In this example, we are using toString()
method to get string representation of a short type object.
Â
2. valueOf()
This method returns a Short instance representing the specified short value. This method should generally be used in preference to the constructor Short(short). It takes a single parameter of short type.
Syntax:
Example:
In this example, we are using valueOf()
method that returns instance of Short class which represents the specified short type.
Â
3. parseShort()
This method returns a short value of the specified string value. We can use it to get a short value from string type value. It takes two parameters: one is String type and second is int type.
Syntax:
Example:
Lets take an example in which we have a string type variable and getting its short value using the parseShort()
method.
Â
4. decode()
This method is used to decode a String into a Short. It accepts decimal, hexadecimal, and octal numbers. It takes a single parameter of String type.
Example:
We can use decode method to decode string type to a short object. See the below example.
Â
5. byteValue()
This method is used to get a primitive type byte value from Short object. It returns the numeric value represented by this object after conversion to type byte.
Syntax:
6. shortValue()
This method returns the value of this Short as a short after a widening primitive conversion.
Syntax:
7. intValue()
The intValue()
method returns the value of this Short as a primitive int type after a widening primitive conversion.
Syntax:
8. longValue()
The longValue()
method returns the value of this Short type as a long type after a widening primitive conversion.
Syntax:
9. doubleValue()
It returns the value of this Short type as a double type after a widening primitive conversion.
Syntax:
10. floatValue()
This method is used to get value of this Short type as a float type after a widening primitive conversion.
Syntax:
Example:
Lets take an example to convert short type to int, long and float type values. In this example, we are using intValue(), floatValue(), doubleValue()
methods.
Â
11. hashCode()
This method is used to get hash code of a short value. It returns an int value of short object.
Syntax:
Example:
Â
12. equals()
The equals()
method compares an object to the specified object. It returns true if objects are same; false otherwise
Syntax:
Example:
We are comparing two byte objects using the equals method that returns true if both the objects are true.
Â
13. compareTo()
This method is used to compare two short objects numerically. It returns 0, if the both short objects are equal. It returns less the 0, if one short object is less than argument object. It returns greater than 0, if one short object is numerically greater than the argument short object.
Syntax:
Example:
In this example, we are comparing two short objects using compareTo method that compares two short objects numerically and returns a numeric value.
Â
14. compare()
It is used to compare two byte values numerically. The value returned is identical to what would be returned by.
Syntax:
Example:
We can use compare method to compare two short values. It returns 0 if both are equal else returns either negative or positive numerical value.
Â
15. reverseBytes()
This method is used to get value obtained by reversing the order of the bytes in the two’s complement representation of the specified short value.
Syntax:
Example:
We are getting a value of a short after reversing its bytes. The returned value is different from the argument value.
Â