by Dinesh Thakur Category: Operators and Expressions

The conversion of a data type which is carried out automatically by the compiler without programmer intervention is called the implicit type conversion. When two variables of different data types are involved in the same expression, the Java compiler uses built-in library functions to trans- form the variables to common data type before evaluating the expression. To decide which variable's data type is to be converted, the Java compiler follows certain rules and converts the lower data type to that of a higher data type. This is known as widening a type.

 

The rules in the sequence in which they are checked are as follows.

1. If one of the operands is double, the other is promoted to double before the operation is carried out.

2. Otherwise, if one of the operands is float, the other is promoted to float before the operation is carried out.

3. Otherwise, if one of the operands is long, the other is promoted to long before the operation is carried out.

4. Otherwise if either operand is int, the other operand is promoted to int.

5. If neither operand is double, float, long or int, both operands are promoted to int.

//Program to Show Implicit Type Conversion 

public class ImplicitTypeConversion
{
         public static void main (String[] args)
     {
           byte  i=10;
           long  j=1+8;
           double k=i*2.5 +j;
           System.out.println("I =" +i);
           System.out.println("J =" +j);
           System.out.println("K =" +k);
      }
}

Implicit Type Conversion in Java Example

About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.



Search Content