With few exceptions, expressions can be nested inside other expressions. Thus, each of these is boolean expression:
true
false
x > 0
(x >= 0) && (x <= 5)
100 == 100
Integer.parseInt("100") == 100
true & (true) & ((true)) & (((true)))
With regards to, say, local variable declaration, you can use any expression (as long as it's grammatically and semantically correct).
So there's really nothing special in something like:
boolean b = (x >= 0) && (x <= 5); // nothing special
In fact, you can also nest assignments like this:
x = y = z; // use judiciously!
Looking at the grammar
Consider the following excerpt from the Java Language Specification:
JLS 14.4 Local Variable Declaration Statements
A local variable declaration statement declares one or more local variable names.
LocalVariableDeclarationStatement:
LocalVariableDeclaration ;
LocalVariableDeclaration:
VariableModifiers Type VariableDeclarators
VariableDeclarators:
VariableDeclarator
VariableDeclarators , VariableDeclarator
VariableDeclarator:
VariableDeclaratorId
VariableDeclaratorId = VariableInitializer
VariableDeclaratorId:
Identifier
VariableDeclaratorId [ ]
VariableInitializer:
Expression
ArrayInitializer
Here we see that a local variable declaration statement allows an optional initialization for each identifier. Each VariableInitializer
is grammatically either an Expression
or an ArrayInitializer
.
Thus, a someType anIdentifer = someExpression;
in this context is simply a local variable statement with an initializer. someExpression
can grammatically be any expression.
Note on ArrayInitializer
ArrayInitializer
are things like { 1, 2, 3 }
and { 4, 5, 6 }
. Note that the ArrayInitializer
grammatical rule is not part of the Expression
production. This is why you can do, say, int[] x = { 1, 2, 3 };
in a declaration with initializer, but not later x = { 4, 5, 6 };
in an assignment.