char to double Java


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

The double takes 8 bytes of memory and char takes 2 bytes of memory. 2 bytes memory value can be assigned to 8 bytes value and is done implicitly by JVM. But the reverse operation, double to char requires explicit casting.

Following program does the implicit operation where a char is assigned to a double. The implicit casting becomes a simple assignment. This is known as widening conversion or automatic promotion.

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.

Following example on char to double shows the Java syntax of implicit casting.
public class Conversions
{
  public static void main(String args[])
  {
    char ch = 'A';          // 2 bytes
    double d1 = ch;         // 2bytes to double of 8 bytes
    
    System.out.println("char value: " + ch);                // prints A
    System.out.println("Converted double value: " + d1);    // prints 65.0 (ASCII value of A is 65)
  }
}


char to double
Output screenshot of char to double Java

double d1 = ch;

The char ch is implicitly type casted to double d1.

View all for 65 types of Conversions

Leave a Comment

Your email address will not be published.