Boolean Literals

Literal expression of a Boolean (true/false) value in source code.

Syntax

Boolean literals have two forms:

1 true
2 false

Syntax Elements

1 The literal for the value true.

2 The literal for the value false.

such that...

Usage

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.

Examples

Usage in Loop

An "infinite" while loop using the literal true:

while (true) {
	renderGame();
	glSwapBuffers();
	if (checkShouldClose())
		break;
	tickGame();
}

Flipping a 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