Advertisement



< Prev
Next >



Short Wrapper Class





Short wrapper class is used to create an object version of a primitive short value.





class A
{
public static void main(String... ar)
{
Short b1 = new Short((short)10); 	//Casting aprimitiveint to short
System.out.println(b1);			//Unboxing the Short object.


//Reading primitive short out of Short object using shortValue() method
Short b2 = new Short("20"); 		//Passing primitive short as a String
System.out.println(b2.shortValue());
}
}


Output-


10
20



Advertisement




Some important methods of Short wrapper class


Methods Description
int compareTo(Long b) -Returns zero if the invoked Short object contains the value same as b.
-Returns the negative difference if the invoked Short object contains a value smaller than b.
-Returns the positive difference if the invoked Short object contains a value greater than b.
boolean equals(Object ob) Returns a true if invoked Short object has same value as referred by ob, else false.
static Short parseShort(String s) Returns a primitive short value if String, s could be converted to a valid short value.
static Short valueOf(short b) Returns a Short object after converting it from a primitive short value, b.
static Short valueOf(String s) Returns a Short object after converting it from a String, s.
short shortValue() Converts a Short object to a primitive short value and returns it
byte byteValue() Converts a Short object to a primitive byte value and returns it.
int intValue() Converts a Short object to a primitive int value and returns it.
long longValue() Converts a Short object to a primitive long value and returns it..
float floatValue() Converts a Short object to a primitive float value and returns it.
double doubleValue() Converts a Short object to a primitive double value and returns it





Using compareTo() method to compare values in two Short objects.


Method compareTo() takes a Short class type object and it -
class A
{
public static void main(String... ar)
{ 
Short s2 = new Short((short)30); //Casting an primitive int value to short, to pass it to the constructor of Short class.
Short s1 = new Short("30"); //Constructor accepting a String value


System.out.println("Value in s1 = "+ s1);
System.out.println("Value in s2 = "+ s2);

System.out.println("Invoking s1 to compare with s2 : "+ s1.compareTo(s2));

Short s3 = new Short("11");
Short s4 = new Short((short)20);	//casting int to a short value

System.out.println("Value in s3 = "+s3);
System.out.println("Value in s4 = "+s4);

System.out.println("Invoking s3 to compare with s4 : "+ s3.compareTo(s4));

System.out.println("Invoking s4 to compare with s3 : "+ s4.compareTo(s3));

}
}


Output-


Value in s1 = 30
Value in s2 = 30
Invoking s1 to compare with s2 : 0
Value in s3 = 11
Value in s4 = 20
Invoking s3 to compare with s4 : -9
Invoking s4 to compare with s3 : 9





To convert a String to a primitive short value using parseShort() method.


Method parseShort(), takes a string value and parse it to primitive short value.
//Converting String to primitive short value.

import java.util.*;

class A
{
public static void main(String... ar)
{
Short b1 = Short.parseShort("25");
Short b2 = Short.parseShort("-200");
Short b3 = Short.parseShort("32767"); //last positive range of a short value

System.out.println("Primitive short value in b1 : "+ b1);
System.out.println("Primitive short value in b2 : "+ b2);
System.out.println("Primitive short value in b3 : "+ b3);
}
}


Output-


Primitive short value in b1 : 25
Primitive short value in b2 : -200
Primitive short value in b3 : 32767





Exception when converting a String to Short


Exception could be caused -

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement



Notifications



Please check our latest addition

C#, PYTHON and DJANGO


Advertisement