Java short type conversion
In this chapter you will learn:
- How to convert Short to byte, double, float, int, long and short
- How to decode a string to short value
- How to convert string to a short value
- How to convert short value to string
Convert Short to byte, double, float, int, long and short
The following list have the methods which we can use to convert short value.
byte byteValue()
returns the value of this Short as a byte.double doubleValue()
returns the value of this Short as a double.float floatValue()
returns the value of this Short as a float.int intValue()
returns the value of this Short as an int.long longValue()
returns the value of this Short as a long.short shortValue()
returns the value of this Short as a short.
public class Main {
public static void main(String[] args) {
Short shortObject = new Short("10");
byte b = shortObject.byteValue();
System.out.println("byte:"+b);
/*j a va2 s.c o m*/
short s = shortObject.shortValue();
System.out.println("short:"+s);
int i = shortObject.intValue();
System.out.println("int:"+i);
float f = shortObject.floatValue();
System.out.println("float"+f);
double d = shortObject.doubleValue();
System.out.println("double:"+d);
long l = shortObject.longValue();
System.out.println("long:"+l);
}
}
The output:
Decode a string to short value
Short.decode(String nm)
decodes a String into a Short.
It accepts decimal, hexadecimal, and octal numbers given by the following grammar:
+/- 0x HexDigits
+/- 0X HexDigits
+/- # HexDigits
+/- 0 OctalDigits
Small demo to illustrates the decode method.
public class Main {
public static void main(String[] args) {
// j a va 2 s. c o m
System.out.println("Decimal 10:"+Short.decode("10"));
System.out.println("Octal 10:"+Short.decode("010"));
System.out.println("Hex F:"+Short.decode("0XF"));
System.out.println("Negative Hex F:"+Short.decode("-0XF"));
}
}
The output:
Convert string to a short value
Short class has the following methods we can use to convert a string to a short type value.
static short parseShort(String s)
parses the string argument as a signed decimal short.static short parseShort(String s, int radix)
parses the string argument as a signed short in the radix specified by the second argument.static Short valueOf(short s)
returns a Short instance representing the specified short value.static Short valueOf(String s)
returns a Short object holding the value given by the specified String.static Short valueOf(String s, int radix)
returns a Short object holding the value extracted from the specified String when parsed with the radix given by the second argument.
The following code demonstrates the methods listed above.
public class Main {
public static void main(String[] args) {
/*from j a va2s . c om*/
System.out.println(Short.parseShort("10"));
System.out.println(Short.parseShort("10",8));
System.out.println(Short.valueOf("10"));
System.out.println(Short.valueOf("10",8));
}
}
The output:
Convert short value to string
To convert short value to string we can use the following methods.
String toString()
returns a String object representing this Short's value.static String toString(short s)
returns a new String object representing the specified short.
public class Main {
public static void main(String[] args) {
Short shortValue = new Short("10");
System.out.println(shortValue.toString());
//from j a va 2s. c o m
System.out.println(Short.toString((short)10));
}
}
The output:
Next chapter...
What you will learn in the next chapter:
- Java int type introduction
- What is octal integer and how to use it in Java
- What is hexadecimal integer and how to use it in Java
- Find out Max, min value of an integer type variable
- How to create Integer using its constructors
- How to compare two integer values
- How to reverse and rotate the integer in binary format
- How to bit oriented operation on int type value
- How to get the sign of an Integer
- How to get the leading and trailing zeros
Home » Java Tutorial » Data Types