Converting of types. Automatic type cast in expressions. Automatic advancement in expressions
1. What is explicit and automatic type casting in expressions?
Casting of types in expressions can occur in cases where of one type of variable need to assign value of the variable of another type. Details of the Java primitive types is described here. Using the variables is described here.
The casting of types can be explicit and automatic. When the explicit type casting, then the operation of casting is given explicitly.
The following two conditions are needed for the automatic types casting:
- both types must be compatible;
- length of the source type (source type) must be less than the length of the target type (type of the receiver).
2. How does the an explicit type casting in the expressions? Examples
The explicit casting of types allows the assignment of incompatible types. The general form of an explicit casting is:
target_type – a type, in which is necessary to convert the specified value.
Examples of an explicit casting of types.
// explicit type casting in expressions byte b; int a; double d; float f; d = -39.9203; a = (int)d; // a = -39 f = (float)d; // f = -39.9203 b = (byte)d; // b = -39 d = 302930932; b = (byte)d; // b = -12 - truncation of the value a = -27; b = (byte)a; // b = -27
3. Examples of automatic types cast
Example 1. Automatic cast of the integer types.
// automatic cast of integer types int a; byte b; short sh; b = -23; a = b; // a = -23 - automatic type cast sh = -150; a = sh; // a = -150 long l = 200; // Error: "Type mismatch: cannot convert from long to int" // a = l; l = b; // l = -23 l = sh; // l = -150 char c = 'Z'; a = c; // a = 90 - the code of character 'Z' boolean b1 = false; //a = b1; - error, incompatible types
Example 2. Automatic cast of floating point types.
// automatic cast of floating point types float f; double d; f = 3.409033f; d = f; // d = 3.409033
Example 3. Automatic cast of mixed types. This case is possible if the integer variable is assigned into the floating point variable.
// automatic cast of mixed types int a; float f; double d; a = 28; d = a; // d = 28.0 f = a; // f = 28.0 // Error: Type mismatch: cannot convert from float to int // a = f;
4. How does the automatic advancement of types in expressions?
Automatic advancement of types occurs in expressions. In this case, the values which appear in expressions are automatically promoted into the types with a more wide range of values.
At automatic advancement of types in expressions:
- if one of the integer operands is of type “int”, the value of all types “byte”, “short” and “char” are advanced to “int”;
- if one of the integer operands is of type “long”, the expression is advanced to “long”;
- if one of the operands is of type “float”, the type of all expression will be “float” (if no operands of type double);
- if one of the operands is of type “double“, the type of all expression will be also “double“.
5. Example. Advancement of a type “byte” to “int” without variables of type “int” (long)
// advancement of types in expressions // byte -> int byte b; b = 1000 / 20; // b = 50, it works, as the result is placed in the "byte" type
The example above works correctly, since:
- the result is placed (is compatible) into a type “byte“;
- no operands of type int.
In the above example, a value of 1000 exceeds the range of values of “byte” type. First, the number 1000 is of type int. But the result is
1000 / 20 = 50
of type “byte“. This result can be saved into in the variable b correctly.
If you write:
byte b; b = 100000 / 20; // error, because the result is not placed in the type "byte"
it will compile error with the message:
Type mismatch: cannot convert from int to byte
In this case, the result is not placed in a “byte” type:
100000 / 20 = 5000
Then this number (5000) will automatically become of type “int“, the compiler generates an error message.
If you make an explicit conversion:
byte b; b = (byte) (100000 / 20); // b = -120
then the result 5000 of type “int” is converted into a type “byte“. As is known, a variable of type int is 32 bits, and a variable of type byte is 8 bits. The value of the int variable is truncated. And we have what we have (b = -120).
The above examples apply to the variables of short and char types.
6. Example 2. Advancement of a “byte” type to “int” type, in which the expression contains a variable operand of type “int“
// byte -> int byte b; int d; d = 20; // Error, the result has type int, since the variable d is of type int // b = 1000 / d;
In the above example, the variable d is used in the expression of type int. Therefore, the compiler generates an error message:
Type mismatch: cannot convert from int to byte
This means that the result is of type “int” (but not “byte“) even if the value is placed in a range of values of type “byte“. Because the variable-operand d of type int is used in the expression.
If you implement explicit type conversion the result will be correct:
// advancement of types in expressions // byte -> int byte b; int d; d = 20; b = (byte)(1000 / d); // b = 50 - works correctly
7. Example. Advancement of type “int” in the “long” type
Example of advancement to a “long” of “int” types. If either operand has type “long”, then the whole expression is advanced to type “long”.
int d; long l; d = 10000 * 200; // it works, d = 2000000 // Error! Type mismatch: cannot convert from long to int // d = 1L * 2L; - operands 1L and 2L are of type "long" l = 100; // error, one of the operands has type "long" // d = l * 2;
8. Example. Advancement to “float” type
This example shows that if one of the operands is a “float” type, then the whole expression advances to the “float” type (if there is no “double” type).
int d; float f; d = (int)(2.5 * 4); // it works, d = 10, there is an explicit conversion from float to int f = 2.5f; d = (int)(f * 4); // it also works, d = 10 // error: cannot convert from float to int // d = 2.5f * 4; // d = f * 4;
9. Example 5. Advancement to “double” type
In the example is shown the rule:
– if either operand has type “double“, the type of the entire expression advances to “double“.
int a; float f; double d; a = 20; d = 20.0; // error: cannot convert from double to float // f = a + d; f = (float)(a + d); // it works: f = 40.0