short to float Java

After knowing primitive data types and Java rules of Data Type Casting (Type Conversion), let us cast short to float.

The short takes 2 byte of memory and float takes 4 bytes of memory. Assignment 2 bytes of memory to 4 byte of memory is done implicitly by JVM. This is known as widening conversion.

See the data types order to know the brief rules of casting.

byte –> short –> int –> long –> float –> double

The left-side value can be assigned to any right-side value and is done implicitly. The reverse like int to short requires explicit casting.

Examples of implicit casting

short x = 10;
int y = x;

byte x = 10;
short y = x;

Following example on short to float explains Java syntax of implicit casting where a short value is assigned to a float.
public class Conversions
{
  public static void main(String args[])
  {
    short s1 = 10;                  // 2 bytes
    float f1 = s1;                  // 2 bytes assigned to 4 bytes

    System.out.println("short value: " + s1);                   // prints 10
    System.out.println("Converted float value: " + f1);         // prints 10.0
  }
}


short to float
Output screenshot of short to float Java

float f1 = s1;

The short s1 is assigned to float f1. This is done implicitly by JVM.

View all for 65 types of Conversions

Leave a Comment

Your email address will not be published.