Java - Short class with Examples



Introduction

The Java 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.

Class Declaration

Following is the declaration for java.lang.Short class −

public final class Short
   extends Number
      implements Comparable<Short>

Field

Following are the fields for java.lang.Short class −

  • static short MAX_VALUE − This is the constant holding the maximum value a short can have, 215-1.

  • static short MIN_VALUE − This is the constant holding the minimum value a short can have, -215.

  • static int SIZE − This is the number of bits used to represent a short value in two's complement binary form.

  • static Class<Short> TYPE − This is the Class instance representing the primitive type short.

Class constructors

Sr.No.Constructor & Description
1

Short(short value)

This constructs a newly allocated Short object that represents the specified short value.

2

Short(String s)

This constructs a newly allocated Short object that represents the short value indicated by the String parameter.

Class methods

Sr.No.Method & Description
1byte byteValue()

This method returns the value of this Short as a byte.

2int compareTo(Short anotherShort)

This method compares two Short objects numerically.

3static Short decode(String nm)

This method decodes a String into a Short.

4double doubleValue()

This method returns the value of this Short as a double.

5boolean equals(Object obj)

This method compares this object to the specified object.

6float floatValue()

This method returns the value of this Short as a float.

7int hashCode()

This method returns a hash code for this Short.

8int intValue()

This method returns the value of this Short as an int.

9long longValue()

This method returns the value of this Short as a long.

10static short parseShort(String s)

This method parses the string argument as a signed decimal short.

11static short reverseBytes(short i)

This method returns the value obtained by reversing the order of the bytes in the two's complement representation of the specified short value.

12short shortValue()

This method returns the value of this Short as a short.

13String toString()

This method returns a String object representing this Short's value.

14static String toString(short s)

This method returns a new String object representing the specified short.

15static Short valueOf(short s)

This method returns a Short instance representing the specified short value.

16static Short valueOf(String s)

This method returns a Short instance representing the specified short value.

17static Short valueOf(String s, int radix)

This method returns a Short object holding the value extracted from the specified String when parsed with the radix given by the second argument.

Methods inherited

This class inherits methods from the following classes −

  • java.lang.Object

Example

The following example shows the usage of Short class to get short from a string.

package com.tutorialspoint;

public class ShortDemo {

   public static void main(String[] args) {

      // create a String s and assign value to it
      String s = "+120";

      // create a Short object i
      Short i;

      // get the value of short from string
      i = Short.valueOf(s);

      // print the value
      System.out.println( "Short value of string " + s + " is " + i );
   }
}

Let us compile and run the above program, this will produce the following result −

Short value of string +120 is 120