New Tutorials:   TKINTER    KOTLIN    JAVASCRIPT    SASS/SCSS    PL/SQL    Matplotlib    C++ Programs
See the Tutorial List

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:

public final class Short extends Number implements Comparable<Short>

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:

	
public String toString(short b)
	

Example:

In this example, we are using toString() method to get string representation of a short type object.

	
public class ShortDemo1 
{ 
    public static void main(String[] args)  
	{ 
        short a = 45;
		String b ="25";
		Short obj = new Short(a);
		Short obj1 = new Short(b);
		System.out.println("toString(a) = " + Short.toString(a));
	}
}
	

short-class

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:

	
public static Short valueOf(short b)
	

Example:

In this example, we are using valueOf() method that returns instance of Short class which represents the specified short type.

	
public class ShortDemo1 
{ 
    public static void main(String[] args)  
	{ 
        short a = 45;
		String b ="25";
		Short obj = new Short(a);
		Short obj1 = new Short(b);
		Short x = Short.valueOf(a); 
		System.out.println("valueOf(a) = " + x); 
        x = Short.valueOf(b); 
		System.out.println("ValueOf(b) = " + x); 
        x = Short.valueOf(b, 6); 
		System.out.println("ValueOf(b,6) = " + x); 
	}
}
	

short-class-example

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:

	
public static short parseShort(String val, int radix) throws NumberFormatException
	

Example:

Lets take an example in which we have a string type variable and getting its short value using the parseShort() method.

	
public class ShortDemo1 
{ 
    public static void main(String[] args)  
	{ 
        short a = 45;
		String b ="25";
		Short obj = new Short(a);
		Short obj1 = new Short(b);
		Short x = Short.parseShort(b); 
		System.out.println("parseShort(b) = " + x);  
        x = Short.valueOf(b, 6); 
		System.out.println("parseShort(b,6) = " + x); 
	}
}
	

short-class-example

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.

	
public static Short decode(String s) throws NumberFormatException
	

Example:

We can use decode method to decode string type to a short object. See the below example.

	
public class ShortDemo1 
{ 
    public static void main(String[] args)  
	{ 
        String a = "25"; 
        String b = "007"; 
        String c = "0x0f"; 
        Short x = Short.decode(a); 
		System.out.println("decod(45) = " + x); 
        x = Short.decode(b); 
		System.out.println("decode(005) = " + x); 
        x = Short.decode(c); 
		System.out.println("decode(0x0f) = " + x); 
	}
}
	

short-class-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:

	
public byte byteValue()
	

6. shortValue()

This method returns the value of this Short as a short after a widening primitive conversion.

Syntax:

	
public short shortValue()
	

7. intValue()

The intValue() method returns the value of this Short as a primitive int type after a widening primitive conversion.

Syntax:

	
Syntax : public int intValue()
	

8. longValue()

The longValue() method returns the value of this Short type as a long type after a widening primitive conversion.

Syntax:

	
public long longValue()
	

9. doubleValue()

It returns the value of this Short type as a double type after a widening primitive conversion.

Syntax:

	
public double doubleValue()
	

10. floatValue()

This method is used to get value of this Short type as a float type after a widening primitive conversion.

Syntax:

	
public float floatValue()
	

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.

	
public class ShortDemo1 
{ 
    public static void main(String[] args)  
	{ 
		short a = 75; 
        Short obj = new Short(a);
		System.out.println("bytevalue(obj) = " + obj.byteValue()); 
		System.out.println("shortvalue(obj) = " + obj.shortValue()); 
		System.out.println("intvalue(obj) = " + obj.intValue()); 
		System.out.println("longvalue(obj) = " + obj.longValue()); 
		System.out.println("doublevalue(obj) = " + obj.doubleValue()); 
		System.out.println("floatvalue(obj) = " + obj.floatValue()); 
	}
}
	

short-class-example

11. hashCode()

This method is used to get hash code of a short value. It returns an int value of short object.

Syntax:

	
public inthashCode()
	

Example:

	
public class ShortDemo1 
{ 
   public static void main(String[] args)  
	{ 
        short a = 45;
		Short obj = new Short(a);
		int x =obj.hashCode(); 
		System.out.println("hashcode(obj) = " + x); 
	}
}
	

short-class-example

12. equals()

The equals() method compares an object to the specified object. It returns true if objects are same; false otherwise

Syntax:

	
public boolean equals(Object obj)
	

Example:

We are comparing two byte objects using the equals method that returns true if both the objects are true.

	 
public class ShortDemo1 
{ 
   public static void main(String[] args)  
	{ 
        short a = 45;
		String b ="25";
		Short obj = new Short(a);
		Short obj1 = new Short(b);
		boolean z = obj.equals(obj1); 
		System.out.println("obj.equals(obj1) = " + z); 
	}
}
	 

short-class-example

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:

	
public intcompareTo(Short b)
	

Example:

In this example, we are comparing two short objects using compareTo method that compares two short objects numerically and returns a numeric value.

	
public class ShortDemo1 
{ 
   public static void main(String[] args)  
	{ 
        short a = 45;
		String b ="25";
		Short obj = new Short(a);
		Short obj1 = new Short(b);
		int z = obj.compareTo(obj1); 
		System.out.println("obj.compareTo(obj1) = " + z); 
	}
}
	

short-class-example

14. compare()

It is used to compare two byte values numerically. The value returned is identical to what would be returned by.

Syntax:

	
public static int compare(short x,short y)
	

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.

	
public class ShortDemo1 
{ 
   public static void main(String[] args)  
	{ 
        short a = 45;
		String b ="25";
		Short obj = new Short(a);
		Short obj1 = new Short(b);
		int z = Short.compare(obj, obj1); 
		System.out.println("compare(obj, obj1) = " + z); 
	}
}
	

short-class-example

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:

	
public static short reverseBytes(short val)
	

Example:

We are getting a value of a short after reversing its bytes. The returned value is different from the argument value.

	 
public class ShortDemo1 
{ 
   public static void main(String[] args)  
	{ 
        short a = 25;
		System.out.println("Short.reverseBytes(a) = " + Short.reverseBytes(a)); 
	}
}
	 

short-class-example