Advertisement
Constructor | Description |
---|---|
Short(short s) | Constructor of Short wrapper class takes a primitive short value. |
Short(String str) | Constructor of Short wrapper class also takes a String equivalent of a primitive short value. |
class A
{
public static void main(String... ar)
{
Short b1 = new Short(short(10)); //Casting the default int to short.
Short b2 = new Short("20"); //Passing primitive short as a String.
}
}
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());
}
}
10
20
Advertisement
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 |
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));
}
}
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
//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);
}
}
Primitive short value in b1 : 25
Primitive short value in b2 : -200
Primitive short value in b3 : 32767
//Converting String to object of Short class by passing it to constructor of Short class.
class A
{
public static void main(String... ar)
{
Short b1 = new Short("12g"); //Passing a String value that cannot be converted to a short value.
System.out.println("Primitive short value in s2 : "+ s2);
}
}
Exception in thread "main" java.lang.NumberFormatException: For input string: "1
2g"
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Short.parseShort(Unknown Source)
at java.lang.Short.(Unknown Source)
at A.main(Byte8.java:7)
//Converting String to primitive short value by passing it to of Short class.
class A
{
public static void main(String... ar)
{
short s1 = Short.parseShort("32767"); //last positive range of a short value
short s2 = Short.parseShort("32768"); //Passing a String value beyond the range of short
System.out.println("Primitive short value in s1 : "+ s1);
System.out.println("Primitive short value in s2 : "+ s2);
}
}
Exception in thread "main" java.lang.NumberFormatException: Value out of range.
Value:"32768" Radix:10
at java.lang.Short.parseShort(Unknown Source)
at java.lang.Short.parseShort(Unknown Source)
at A.main(Short3.java:10)
//Converting Short to short, int, long, float, double
class A
{
public static void main(String... ar)
{
Short sh = new Short("100"); // Converting a String argument to wrapped Integer object
System.out.println("Value in wrapped Short object, sh : "+ y);
byte b = sh.byteValue(); //Returns a primitive byte value out of a wrapped Short object
short s= sh.shortValue(); //Returns a primitive short value out of a wrapped Short object
int i = sh.intValue(); //Returns a primitive int value out of a wrapped Short object
long l = sh.longValue(); //Returns a primitive long value out of a wrapped Short object
float f = sh.floatValue(); //Returns a primitive float value out of a wrapped Short object
double d = sh.doubleValue(); //Returns a primitive double value out of a wrapped Short object
System.out.println("byte value of Short object, sh :" + b);
System.out.println("short value of Short object, sh :" + s);
System.out.println("int value of Short object, sh :" + i);
System.out.println("long value of Short object, sh :" + l);
System.out.println("float value of Short object, sh :" + f);
System.out.println("double value of Short object, sh :"+ d);
}
}
Value in wrapped Short object, sh : 100
byte value of Short object, sh : 100
short value of Short object, sh: 100
int value of Short object, sh : 100
long value of Short object, sh : 100
float value of Short object, sh : 100.0
double value of Short object, sh : 100.0
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement