byte to float Java


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

The byte data type takes one byte of memory and float takes 4 bytes. Assigning 1 byte memory value to 4 bytes memory goes implicitly in Java. This is also known as implicit casting or widening conversion. It is easy casting to JVM without extra processing overload like placing smaller bottle water in a bigger bottle.

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

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

Examples:

       short x = 10;
       int y = x;

       byte x = 10;
       short y = x;
Following program on byte to float conversion explains the syntax of implicit casting in Java.
public class Conversions
{
 public static void main(String args[])
 {
   byte b1 = 10;               // 1 byte  
   float f1 = b1;              //  one byte to 4 bytes, goes implicitly
   
   System.out.println("byte value: " + b1);                //  prints 10
   System.out.println("Converted float value: " + f1);     //  prints 10.0
 }
}


byte to float
Output screenshot of byte to float

1 byte value b1 is assigned to 4 bytes f1 and is type casted implicitly.

View all for 65 types of Conversions

Leave a Comment

Your email address will not be published.