This example shows how to convert Short object to numeric primitive types byte, short, int, long, float, and double.
How to convert Short object to numeric primitive types in Java?
Use the xxxValue method of Short wrapper class to convert a Short object to the primitive numeric types where the xxx is a primitive type you want to convert to as given below.
1) Convert Short to byte
Use the byteValue
method of the Short wrapper class to convert from Short to byte primitive value.
1 |
public byte byteValue() |
This method returns the value of the Short object as a byte primitive.
2) Convert Short to short
Use the shortValue
method of the Short wrapper class to convert from Short to short primitive value.
1 |
public short shortValue() |
This method returns the value of the Short object as a short primitive.
3) Convert Short to int
Use the intValue
method of the Short wrapper class to convert from Short to int primitive value.
1 |
public int intValue() |
This method returns the value of the Short object as an int primitive.
4) Convert Short to long
Use the longValue
method of the Short wrapper class to convert from Short to a long primitive value.
1 |
public long longValue() |
This method returns the value of the Short object as long primitive.
5) Convert Short to float
Use the floatValue
method of the Short wrapper class to convert from Short to a float primitive value.
1 |
public float floatValue() |
This method returns the value of the Short object as float primitive.
6) Convert Short to double
Use the doubleValue
method of the Short wrapper class to convert from Short to double primitive value.
1 |
public double doubleValue() |
This method returns the value of the Short object as a double primitive.
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
package com.javacodeexamples.basic.wrapperclasses; public class ConvertShortToPrimitiveExample { public static void main(String[] args) { Short s = new Short("26"); System.out.println("Short to byte: " + s.byteValue() ); System.out.println("Short to short: " + s.shortValue() ); System.out.println("Short to int: " + s.intValue() ); System.out.println("Short to long: " + s.longValue() ); System.out.println("Short to float: " + s.floatValue() ); System.out.println("Short to double: " + s.doubleValue() ); } } |
Output
1 2 3 4 5 6 |
Short to byte: 26 Short to short: 26 Short to int: 26 Short to long: 26 Short to float: 26.0 Short to double: 26.0 |
This example is a part of the Java Basic Examples and Java Type conversion Tutorial.
Please let me know your views in the comments section below.