Types of the left hand side variable and right hand side expression in an assignment may differ from each other as long as they are type-compatible with each other. For example all the primitive types excluding boolean are type-compatible with each other. When value of an expression is assigned to a variable, it should be converted to the type of the variable on the left hand side. The type conversion may take place automatically or we may have to convert the type explicitly using typecast operator.
Type conversions can be classified as:
Widening conversion takes place when we assign a lower type expression to a higher type. There is no possibility of any data/precision loss in a widening conversion so it takes place implicitly/automatically.
Examples: The following examples will clarify this.
Suppose following variables are declared in a method/block:
int i ; byte b= 3; short s = 16; char c = 65; long l; float f; double d;
The following expressions are valid as widening conversion takes place automatically:
i = b; l = s; f = b; d = c;
Narrowing Conversions
Narrowing conversion takes place when we assign a higher type expression to a lower type.
There is possibility of data/precision loss in a narrowing conversion. So normally a narrowing conversion does not take place implicitly/automatically. The programmer needs to explicitly typecast the higher type to lower type so that he/she is aware of the possibility of data/precision loss. However, there are some cases where even narrowing conversion takes place automatically.
Simple assignments always need explicit type conversion.
Examples: The following examples will clarify this.
Suppose following variables are declared in a method/block:
int i = 10 ; byte b; short s; char c; long l = 23; float f = 18.1f; double d = 5.77;
The following expressions are not valid, as narrowing conversions need explicit typecast:
b = i; s = l; b = f; c = d;
To use these expressions you need to do explicit typecasting as follows:
b = (byte) i; s = (short) l; b = (byte) f; c = (char) d;
Type conversions can be classified as:
- Widening (or Broadening or UpCasting) Type Conversions
- Narrowing (or DownCasting) Type Conversions
Widening conversion takes place when we assign a lower type expression to a higher type. There is no possibility of any data/precision loss in a widening conversion so it takes place implicitly/automatically.
Examples: The following examples will clarify this.
Suppose following variables are declared in a method/block:
int i ; byte b= 3; short s = 16; char c = 65; long l; float f; double d;
The following expressions are valid as widening conversion takes place automatically:
i = b; l = s; f = b; d = c;
Narrowing Conversions
Narrowing conversion takes place when we assign a higher type expression to a lower type.
There is possibility of data/precision loss in a narrowing conversion. So normally a narrowing conversion does not take place implicitly/automatically. The programmer needs to explicitly typecast the higher type to lower type so that he/she is aware of the possibility of data/precision loss. However, there are some cases where even narrowing conversion takes place automatically.
Simple assignments always need explicit type conversion.
Examples: The following examples will clarify this.
Suppose following variables are declared in a method/block:
int i = 10 ; byte b; short s; char c; long l = 23; float f = 18.1f; double d = 5.77;
The following expressions are not valid, as narrowing conversions need explicit typecast:
b = i; s = l; b = f; c = d;
To use these expressions you need to do explicit typecasting as follows:
b = (byte) i; s = (short) l; b = (byte) f; c = (char) d;