Java Reference
In-Depth Information
You get a compile-time error when you try to assign the same value 5 to a byte variable b1 , as shown in the
following snippet of code:
byte b1;
byte b2 = 2;
byte b3 = 3;
b1 = b2 + b3; // A compile-time error. Trying to assign 5 to b1
Why does the above snippet of code result in a compile-time error? Do the expressions b1 = 5 and b1 = b2 + b3
not have the same effect of assigning 5 to the variable b1? Yes, the effect would be the same. However, the rules that
govern the assignment operation are different in two cases. In the expression b1 = 5 , the assignment is governed by
the rule that any int literal between -128 and 127 can be assigned to a byte variable. Because 5 is between -128 and
127, the assignment of b1 = 5 is valid. The second assignment, b1 = b2 + b3 , is governed by the fourth rule for the
determination of the data type of an arithmetic expression, which uses addition operator. Because both operands in
the expression b2 + b3 are of byte types, the operands b2 and b3 are first converted to the int data type, and then the
expression b2 + b3 becomes of the int type. Because the data type of b1 is byte , which is smaller than the int data
type of the expression b2 + b3, the assignment b1 = b2 + b3 (that is, int to byte ) is not compatible, and that is the
reason it generates an error. In such a case, you need to cast the result of the right-hand expression to the data type of
the left-hand operand.
b1 = (byte)(b2 + b3); // Ok now
Beginners may try to write the above statement of code like so:
b1 = (byte) b2 + b3; // An error again
The two expressions (byte)(b2 + b3) and (byte)b2 + b3 are not the same. In the expression (byte) (b2 + b3) ,
first b2 and b3 are promoted to int data type, and then an addition is performed, which results in a value 5 of the int
data type. Then, the int value 5 is cast to byte and assigned to b1 .
In the expression (byte)b2 + b3 , first, b2 is cast to byte . Note that this cast is redundant since b2 is already of
type byte ; both b2 and b3 are promoted to int data type; and the whole expression of (byte)b2 + b3 is of type int .
Since int to byte assignment is not permitted, the expression would not compile.
The error produced by the second expression, ( byte ) b2 + b3 , raises an interesting question. Why did Java not
first compute b2 + b3 in (byte)b2 + b3 and then applied ( byte ) to the result? Because there were two operations to
be done, one being the cast to byte and another being the addition of b2 and b3 , Java did the cast on b2 first and the
addition second. The decision to perform the cast first followed by the addition was not arbitrary. Each operator in
Java has a precedence order. The operator, which has higher precedence, is evaluated first before the operators having
lower precedence order. The cast operator has higher precedence than the addition operator. This is the reason
that (byte)b2 was evaluated first in (byte)b2 + b3 . You can always override the precedence of operators using
parentheses; note how I overrode the precedence of the cast operator by using parentheses in expression
(byte)(b2 + b3) . Let's consider another example.
byte b1;
b1 = 3 + 2; // Will this line of code compile?
Will the expression b1 = 3 + 2 compile? If you apply the fourth rule for determining the data type of this
expression, it should not compile because 3 and 2 are int literals. The expression 3 + 2 is of type int . Because int is
not assignment compatible to byte , the expression b1 = 3 + 2 should give an error. However, this assumption is wrong
and the expression b1 = 3 + 2 will compile fine. In this case, the assignment proceeds as follows: the operands 3 and 2
are constants, so their values are known at compile time. Therefore, the compiler computes the result of the expression
3 + 2 at the time of compilation and replaces 3 + 2 by its result, 5 . The expression b1 = 3 + 2 is replaced by b1 = 5
 
Search WWH ::




Custom Search