Casting Operations

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…

byte to double Java

After knowing primitive data types and Java rules of Data Type Casting (Type Conversion), let us cast byte to double. The byte takes 1 byte of memory and double takes 8 bytes of memory. Assignment 1 byte value to 4 bytes is done implicitly by the JVM. JVM does not…

byte to char Java

After knowing primitive data types and Java rules of Data Type Casting (Type Conversion), let us cast byte to char as an example. The byte takes 1 byte of memory and char takes 2 bytes of memory. Here, casting rules do not work like 1 byte value can be assigned…

byte to boolean Java

After knowing primitive data types and Java rules of Data Type Casting (Type Conversion), let us cast byte to boolean as an example and see the problems of doing so. The data type boolean is incompatible for converting into any other data type. That is, a boolean value cannot be…

long to short java

After knowing primitive data types and Java rules of Data Type Casting (Type Conversion), let us cast long to short as an example. The long takes 8 bytes of memory and short takes 2 bytes of memory. Assigning 8 bytes of memory to 2 bytes of memory requires explicit casting.…

long to byte Java

After knowing primitive data types and Java rules of Data Type Casting (Type Conversion), let us cast long to byte as an example. The byte takes 1 byte of memory and long takes 8 bytes of memory. Assigning 8 bytes of memory to 1 byte of memory requires explicit casting.…

long to float Java

After knowing primitive data types and Java rules of Data Type Casting (Type Conversion), let us cast long to float as an example. A float carries a mantissa part (value of decimal point) where as long takes a whole number. Assignment of long to float is done implicitly. Observe the…

long to double Java

After knowing primitive data types and Java rules of Data Type Casting (Type Conversion), let us cast long to double as an example. The long and double, each takes 8 bytes of memory. The long holds a whole number and double holds a floating-point number. Assignment of long to double…

long to char Java

After knowing primitive data types and Java rules of Data Type Casting (Type Conversion), let us cast long to char as an example. The long takes 8 bytes of memory and char takes 2 bytes of memory. Assignment of 8 bytes of memory to 2 bytes of memory requires explicit…

long to boolean Java

After knowing primitive data types and Java rules of Data Type Casting (Type Conversion), let us cast long to boolean as an example. The data type boolean is incompatible for converting into any other data type. That is, a boolean value cannot be converted (or assigned to) into any other…

short to byte Java

After knowing primitive data types and Java rules of Data Type Casting (Type Conversion), let us cast short to byte as an example. The byte takes 1 byte of memory and short takes 2 bytes of memory. Assigning 2 bytes of memory to 1 byte of memory requires explicit casting.…

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…

short to double Java

After knowing primitive data types and Java rules of Data Type Casting (Type Conversion), let us cast short to double as an example. The short takes 2 byte of memory and double takes 8 bytes of memory. Assignment 2 bytes of memory to 8 byte of memory is done by…

short to char Java

After knowing primitive data types and Java rules of Data Type Casting (Type Conversion), let us cast short to char. The char and short, each takes 2 byte of memory. Assignment of short to char requires explicit casting as short can take negative values also but char does not. See…