Short Wrapper Class in Java

The Short class wraps a value of primitive type short in an object. An object of type Short contains a single field whose type is short.
In addition, 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 useful when dealing with a short.

Short class Constructors

  • Short(short value) - Constructs a newly allocated Short object that represents the specified short value.
  • Short(String s) - Constructs a newly allocated Short object that represents the short value indicated by the String parameter.
Example:
short b = 55;
String bb = "45";
 
// Construct two Short objects
Short x = new Short(b);
Short y = new Short(bb);

Short class Methods

The class diagram shows a list of APIs/Methods that Short class provides.
In this post, we will learn a few important methods of the Short wrapper class.
Example: This program demonstrates usage of Short wrapper class methods.
package com.javaguides.corejava.wrapperclasses;

/**
 * This class to demonstrate Short wrapper class methods
 * @author javaguides.net
 *
 */
public class ShortClassExample {
 public static void main(String[] args) {
  short b = 55;
        String bb = "45";
         
        // Construct two Short objects
        Short x = new Short(b);
        Short y = new Short(bb);
 
        // toString()
        System.out.println("toString(b) = " + Short.toString(b));
 
        // valueOf()
        // return Short object
        Short z = Short.valueOf(b);
        System.out.println("valueOf(b) = " + z);
        z = Short.valueOf(bb);
        System.out.println("ValueOf(bb) = " + z);
        z = Short.valueOf(bb, 6);
        System.out.println("ValueOf(bb,6) = " + z);
 
        // parseShort()
        // return primitive short value
        short zz = Short.parseShort(bb);
        System.out.println("parseShort(bb) = " + zz);
        zz = Short.parseShort(bb, 6);
        System.out.println("parseShort(bb,6) = " + zz);
         
        //decode()
        String decimal = "45";
        String octal = "005";
        String hex = "0x0f";
         
        Short dec = Short.decode(decimal);
        System.out.println("decode(45) = " + dec);
        dec = Short.decode(octal);
        System.out.println("decode(005) = " + dec);
        dec = Short.decode(hex);
        System.out.println("decode(0x0f) = " + dec);
 
        System.out.println("bytevalue(x) = " + x.byteValue());
        System.out.println("shortvalue(x) = " + x.shortValue());
        System.out.println("intvalue(x) = " + x.intValue());
        System.out.println("longvalue(x) = " + x.longValue());
        System.out.println("doublevalue(x) = " + x.doubleValue());
        System.out.println("floatvalue(x) = " + x.floatValue());
         
        int hash = x.hashCode();
        System.out.println("hashcode(x) = " + hash);
         
        boolean eq = x.equals(y);
        System.out.println("x.equals(y) = " + eq);
         
        int e = Short.compare(x, y);
        System.out.println("compare(x,y) = " + e);
         
        int f = x.compareTo(y);
        System.out.println("x.compareTo(y) = " + f);
         
         
        short to_rev = 45;
        System.out.println("Short.reverseBytes(to_rev) = " + Short.reverseBytes(to_rev));
 }
}
Output:
toString(b) = 55
valueOf(b) = 55
ValueOf(bb) = 45
ValueOf(bb,6) = 29
parseShort(bb) = 45
parseShort(bb,6) = 29
decode(45) = 45
decode(005) = 5
decode(0x0f) = 15
bytevalue(x) = 55
shortvalue(x) = 55
intvalue(x) = 55
longvalue(x) = 55
doublevalue(x) = 55.0
floatvalue(x) = 55.0
hashcode(x) = 55
x.equals(y) = false
compare(x,y) = 10
x.compareTo(y) = 10
Short.reverseBytes(to_rev) = 11520

Reference

Comments