Java Reference
In-Depth Information
state = false;
sets the value of the variable state to false .
At this point you can't do much with a boolean variable, other than to set its value to true or false ,
but as you see in the next chapter, boolean variables become much more useful in the context of decision
making in a program, particularly when you can use expressions that produce a result of type boolean .
Several operators combine boolean values, including operators for boolean AND, boolean OR, and
boolean negation (these are && , || , and ! , respectively), as well as comparison operators that produce a
boolean result. Rather than go into these here in the abstract, I defer discussion until the next chapter where
I also go into how you can apply them in practice to alter the sequence of execution in a program.
NOTE Note that variables of type boolean differ from the other primitive data types in that
theycannotbecasttoanyotherbasictype,andtheotherprimitivetypescannotbecasttotype
boolean .
OPERATOR PRECEDENCE
I have already introduced the idea of a pecking order for operators that determines the sequence in which
they are executed in a statement. A simple arithmetic expression such as 3 + 4 * 5 results in the value 23
because the multiply operation is executed first — it takes precedence over the addition operation. I can now
formalize the position by classifying all the operators present in Java according to their precedence. Each
operator in Java has a set priority or precedence in relation to the others, as shown in the following table.
Operators with a higher precedence are executed before those of a lower precedence. Precedence is highest
for operators in the top line in the table, down through to the operators in the bottom line, which have the
lowest precedence. Operators that appear on the same line of the table have the same precedence (see Table
2-10 ) :
TABLE 2-10 : The Associativity of Operator Precedence Groups
OPERATOR PRECEDENCE GROUP
ASSOCIATIVITY
(), [], postfix ++, postfix −−
left
unary +, unary −, prefix ++, prefix −−, ~, !
right
(type), new
left
*, /, %
left
+, −
left
<<, >>, >>>
left
< ,<= , >, >=, instanceof
left
==, !=
left
&
left
^
left
|
left
&&
left
||
left
 
 
Search WWH ::




Custom Search