Skip to content

Commit cbb73ce

Browse files
committed
Type Conversion and Casting.
Signed-off-by: Somesh diwan <[email protected]>
1 parent ab6fb22 commit cbb73ce

File tree

1 file changed

+22
-8
lines changed

1 file changed

+22
-8
lines changed

Section5OperatorExpression/Txt and Images/type_conversion&casting.txt renamed to Section5OperatorExpression/Txt and Images/Type Conversion and Casting.txt

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,26 @@ Type Conversion and Casting:
33
Java’s Automatic Conversions
44
When one type of data is assigned to another type of variable, an automatic type conversion will take place if the
55
following two conditions are met:
6+
67
• The two types are compatible.
78
• The destination type is larger than the source type.
9+
810
Java also performs an automatic type conversion when storing a literal integer constant into variables of type
911
byte, short, long, or char.
1012

11-
Casting Incompatible Types
12-
Although the automatic type conversions are helpful, they will not fulfill all needs. For example, what if you want to
13+
Casting Incompatible Types:
14+
Although the automatic type conversions are helpful, they will not fulfill all needs.
15+
16+
For example, what if you want to
1317
assign an int value to a byte variable? This conversion will not be performed automatically, because a byte is smaller
14-
than an int. This kind of conversion is sometimes called a narrowing conversion, since you are explicitly making the
18+
than an int.
19+
20+
This kind of conversion is sometimes called a narrowing conversion, since you are explicitly making the
1521
value narrower so that it will fit into the target type.
1622

1723
For example, the following fragment casts an int to a byte. If the integer’s value is larger than the range of a byte,
1824
it will be reduced modulo (the remainder of an integer division by the) byte’s range.
25+
1926
int a;
2027
byte b;
2128
b = (byte) a;
@@ -26,19 +33,22 @@ Automatic Type Promotion in Expressions:
2633

2734
int a = 257;
2835
byte b = (byte)a;
36+
2937
When the value 257 is cast into a byte variable, the result is the remainder of the division of 257 by 256
3038
(the range of a byte), which is 1 in this case.
3139

32-
3340
byte a = 40;
3441
byte b = 50;
3542
byte c = 100;
3643
int d = a * b / c;
44+
3745
The result of the intermediate term a * b easily exceeds the range of either of its byte operands.
3846
To handle this kind of problem, Java automatically promotes each byte, short, or char operand to int when evaluating
3947
an expression. This means that the subexpression a*b is performed using integers—not bytes.
48+
4049
byte b = 50;
4150
b = b * 2; // Error! Cannot assign an int to a byte!
51+
4252
The code is attempting to store 50 * 2, a perfectly valid byte value, back into a byte variable. However, because the
4353
operands were automatically promoted to int when the expression was evaluated, the result has also been promoted to int.
4454

@@ -67,7 +77,11 @@ Let’s look closely at the type promotions that occur in this line from the pro
6777
double result = (f * b) + (i / c) - (d * s);
6878

6979
In the first subexpression, f * b, b is promoted to a float and the result of the subexpression is float.
70-
Next, in the subexpression i/c, c is promoted to int, and the result is of type int. Then, in d * s, the value of s is
71-
promoted to double, and the type of the subexpression is double. Finally, these three intermediate values, float, int,
72-
and double, are considered. The outcome of float plus an int is a float. Then the resultant float minus the last double
73-
is promoted to double, which is the type for the final result of the expression.
80+
Next, in the subexpression i/c, c is promoted to int, and the result is of type int.
81+
82+
Then, in d * s, the value of s is promoted to double, and the type of the subexpression is double.
83+
84+
Finally, these three intermediate values, float, int,and double, are considered.
85+
86+
The outcome of float plus an int is a float. Then the resultant float minus the last double is promoted to double,
87+
which is the type for the final result of the expression.

0 commit comments

Comments
 (0)