Data Type Conversion

Type casting is process to convert a variable from one data type to another data type. For example if we want to store a integer value in a float variable then we need to typecast integer into float.

If information is lost in an assignment, the programmer must confirm the assignment with a typecast.

The assignment between short and char requires an explicit cast.

long bigValue = 99L;

int squashed = (int)(bigValue);

long bigval = 6; // 6 is an int type, OK

int smallval = 99L; // 99L is a long, illegal

Promotion and Casting of Expressions

Variables are automatically promoted to a longer form (such as int to long).

Expression is assignment compatible if the variable type is at least as large (the same number of bits) as the expression type.

double z = 12.414F; // 12.414F is float, OK

float z1 = 12.414; // 12.414 is double, illegal

There are tow type of conversion

Implicit: When the type conversion is performed automatically by the compiler without programmers intervention, such type of conversion is known as implicit type conversion

Explicit: The type conversion performed by the programmer by posing the data type of the expression of specific type is known as explicit type conversion. The explicit type conversion is also known as type casting.

Syntax:
(data_type) var_name;
Example:
int =65; char ch=(char)a;