java.lang.Short Class

The Short class wraps the value of primitive data type short into Short object. An object of type Short contains a single field whose type is short. In other words the wrapper classes create objects for primitive data types. The wrapper classes are Boolean, Byte, Character, Short, Integer, Float, Long and Double.

This class has following constructors.

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.

java.lang.Short Class Example
/* Save with file name ShortExample.java */

public class ShortExample
{
	public static void main(String args[])
	{
		//SHORT DECLARATION
		Short b;
		//PRIMITIVE short DECLARATION AND VALUE ASSIGNMENT
		short a = 101;
		//MEMORY ALLOCATION FOR SHORT
		b = new Short(a);
		//SHORT CLASS OUTPUT
		System.out.println();
		System.out.println("Short Class Example");
		System.out.println("=====================");
		System.out.println();
		//RETURNS byte PRIMITIVE DATA TYPE
		System.out.println("byte Value is : "+ b.byteValue());
		//RETURNS short PRIMITIVE DATA TYPE
		System.out.println("short Value is : "+ b.shortValue());
		//RETURNS double PRIMITIVE DATA TYPE
		System.out.println("double Value is : "+ b.doubleValue());
		//RETURNS float PRIMITIVE DATA TYPE
		System.out.println("float Value is : "+ b.floatValue());
		//RETURNS int PRIMITIVE DATA TYPE
		System.out.println("int Value is : "+ b.intValue());
		//RETURNS long PRIMITIVE DATA TYPE
		System.out.println("long Value is : "+ b.longValue());
	}
}
                        
java.lang.Short Class Example 2
/* Save with file name ShortExample2.java */

public class ShortExample2
{
	public static void main(String args[])
	{
		//SHORT DECLARATION
		Short b, b2;
		//PRIMITIVE short DECLARATION AND VALUE ASSIGNMENT
		short a = 101;
		//MEMORY ALLOCATION FOR SHORT
		b = new Short(a);
		b2 = new Short("102");
		//SHORT CLASS OUTPUT
		System.out.println();
		System.out.println("Short Class Example");
		System.out.println("=====================");
		System.out.println();
		System.out.println("b Value is : "+ b.shortValue());
		System.out.println("b2 Value is : "+ b2.shortValue());
		System.out.println();
		System.out.println("b compareTo b2 : "+ b.compareTo(b2));
		System.out.println("b equals b2 : "+ b.equals(b2));
	}
}
                        

In the following example you can learn how to use static methods of Short Class. If you use static methods you need not to create the class instance. You can call static methods using the class name as reference.

java.lang.Short Class Example 3
/* Save with file name ShortExample3.java */

public class ShortExample3
{
	public static void main(String args[])
	{
		//PRIMITIVE short DECLARATION AND VALUE ASSIGNMENT
		short a = 101;
		//SHORT CLASS STATIC METHODS USAGE
		System.out.println("Short Class Example");
		System.out.println("=====================");
		System.out.println();
		//RETURN Short OBJECT WITH THE SPECIFIED PRIMITIVE short DATA TYPE
		System.out.println("valueOf : "+ Short.valueOf(a));
		//RETURN Short OBJECT WITH THE SPECIFIED
		//PRIMITIVE short DATA TYPE AS String
		System.out.println("valueOf : "+ Short.valueOf("103"));
		//RETURN PRIMITIVE short DATA TYPE WITH THE SPECIFIED
		//PRIMITIVE short DATA TYPE AS String
		System.out.println("parseShort : "+ Short.parseShort("105"));
	}
}