In computer science, “type casting” and “type conversion” refers when there is either implicitly or explicitly is a conversion from one data type to another. When both types of expression are compatible with each other, then Data type conversions from one type to another can be carried out Automatically by java compiler. However, there is a technical difference between type conversion and type casting, i.e. type conversion is carried out “automatically” by java compiler while the “type casting” (using a cast operator) is to be explicitly performed by the java programmer.It is usually classified into two categories
a) Implicit type conversion widening conversion
b) Explicit type conversion or compacting conversion
We’ll be covering the following topics in this tutorial:
Definition of Type Conversion (Widening conversion)
Type Conversion can be implemented when two data types are ‘compatible’, that which converts the one data type into another, for example, converting an int into float converting a float into double. The Type Conversion is that which automatically converts the one data type into another by the compiler without programmer intervention. But remember, we can store a large data type into the other. For example, we can’t store a float into int because a float is greater than int. Type Conversion is also called as Promotion of data, because we are converting one lower data type into a higher data type, So this is performed automatically by java compiler. This is known as widening a type.
Type conversion can occur in number of situations like in an assignment statement, in arithmetic expressions involving different data types and in returning value from the function in which the type of the expression differs from the return type.
The rules 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 inti the other operand is promoted to into
5. If neither operand is double, float, long or int,both operands are promoted to into.
Definition of Type Casting (Narrowing conversion)
When a user can convert the one higher data type into a lower data type, then it is called as the typecasting. Remember the compiler performs the type Conversion, but the user does casting on two ‘incompatible’ data types with casting operator ‘()’. For example, converting a float into int always. Remember that when we use the Type Conversion, then it is called the promotion. When we use the typecasting means, when we convert a large data type into another, then it is called as the demotion. When we use typecasting, then we can lose some data. Like we are converting a float into int then it truncates the fractional part from the data like (int) 123.78 will gives use only 123 this truncate the fractional part and returns an only int value.