Java Variable Type Conversion & Type Casting
The value of one primitive data type assigns to the another data type is known as type casting or java variable type conversion.
Here there are 2 types -
1) Automatic type casting(widening casting) = small - 2 - large
Variable of smaller capacity is be assigned to another variable of bigger capacity.
byte->short->char->int->long->float->double
This process is Automatic, and non-explicit is known as Conversion
2) Manual type casting (narrowing casting) = large - 2 - small
Variable of larger capacity is be assigned to another variable of smaller capacitydouble->float->long->int->char->short->byte
int a;
double d = 128.128;
a = (int) d;
In such cases, you have to explicitly specify the type cast operator. This process is known as Type Casting.
In case, you do not specify a type cast operator; the compiler gives an error. Since this rule is enforced by the compiler, it makes the programmer aware that the conversion he is about to do may cause some loss in data and prevents accidental losses.
Example: How Manual Type Casting works
In case, you do not specify a type cast operator; the compiler gives an error. Since this rule is enforced by the compiler, it makes the programmer aware that the conversion he is about to do may cause some loss in data and prevents accidental losses.
Example: How Manual Type Casting works
Output:
0 Comments