Type conversion

Type Conversion is one which converts the one data type into another for example converting a int into float, converting a float into double, etc. In Type Conversion one data type is automatically gets converted into another but remember we can’t store a large data type into smaller data type. E.g. we can’t store a float into int because a float is greater than int. Type Conversion is also called as Promotion of data type because we are converting one lower data type into higher data type. So this is performed automatically by java compilers.

A promotion from on base type to another may occur in an arithmetic expression:
        double x = 3.14 + 10; /* 10 is promoted to a double */ 
        double y = 12; /* 12 is promoted to a double then assigned */ 

If an double is used where an int is expected, Java will generate an error:
        int z = 2.4; Compile-time error: possible loss of precision


Post a Comment