Literal expression of a Boolean (true
/false
)
value in source code.
Boolean literals have two forms:
1 | true |
2 | false |
1 The literal for the value true
.
2 The literal for the value false
.
such that...
null
literal,
Boolean literals must be separated from identifier-like tokens by any
non-zero amount of whitespace or a comment, otherwise they will be
interpreted as a part of the other identifier-like token.
The type of a Boolean literal is always the boolean
type.
Boolean literals can be used in any context that a boolean expression
is acceptable.
An "infinite" while
loop using the literal true
:
while (true) {
renderGame();
glSwapBuffers();
if (checkShouldClose())
break;
tickGame();
}
boolean
A simple statement that flips the value of a boolean
variable can be made with the literal true
:
boolean x = false;
x ^= true; // Flips x; x is now true
x ^= true; // Flips x; x is now false
x ^= true; // x is true
x ^= true; // x is false
x ^= true; // x is true
if (x) {
System.out.println("X is true");
}
Output:
X is true