Convert short to float in Java
In this Java core tutorial, we learn how to convert short value into float value in Java via different solutions.
Assign short variable to float variable in Java
ConvertShortToFloatExample1.java
public class ConvertShortToFloatExample1 {
public static void main(String[] args) {
short value1 = 10;
float value2 = value1;
System.out.println("short value: " + value1);
System.out.println("float value: " + value2);
}
}
short value: 10
float value: 10.0
Using Float.valueOf() method to instantiate float from a short value
ConvertShortToFloatExample2.java
public class ConvertShortToFloatExample2 {
public static void main(String[] args) {
short value1 = 20;
float value2 = Float.valueOf(value1);
System.out.println("short value: " + value1);
System.out.println("float value: " + value2);
}
}
short value: 20
float value: 20.0
Using Short.floatValue() method to get float value from a Short object
ConvertShortToFloatExample3.java
public class ConvertShortToFloatExample3 {
public static void main(String[] args) {
Short value1 = Short.valueOf((short)30);
float value2 = value1.floatValue();
System.out.println("short value: " + value1);
System.out.println("float value: " + value2);
}
}
short value: 30
float value: 30.0
Happy Coding 😊