Boolean
Booleans are often used in Java programs. We can use the literals "true" and "false." We often use booleans inside if
-statements, or while
-loops.
It is possible to invert the value of a boolean with a simple expression. Booleans are often useful as local variables.
In this program we use the literal constants true and false. Often we assign a boolean to true or false as we declare it. Here we test for truth in two ifs.
if
-statement then detects "value" is true.public class Program { public static void main(String[] args) { // Test true and false booleans. boolean value = true; if (value) { System.out.println("A"); } value = false; if (!value) { System.out.println("B"); } } }A B
We can negate (or invert, or flip) a boolean value. This changes true to false, and false to true. We use the exclamation mark to perform this operation.
public class Program { public static void main(String[] args) { boolean value = true; System.out.println(value); // Flip from true to false. value = !value; System.out.println(value); // Flip back to true from false. value = !value; System.out.println(value); } }true false true
A boolean variable can store the result of an expression evaluation. It can then be reused, without reevaluating the expression.
largeAndBlue
.public class Program { public static void main(String[] args) { int size = 100; String color = "blue"; // Use expression to compute a boolean value. boolean largeAndBlue = size >= 50 && color == "blue"; // Test our boolean. if (largeAndBlue) { System.out.println("Large and blue = " + largeAndBlue); } } }Large and blue = true
Boolean
argumentsMethods often receive boolean arguments. We can pass them anything that evaluates to a boolean—this includes the constants true and false.
public class Program { static void display(boolean console) { // Display a message if boolean argument is true. if (console) { System.out.println("The cat is cute."); } } public static void main(String[] args) { // A message is displayed. display(true); // These calls do not print anything to the console. display(false); display(false); } }The cat is cute.
Here we see a method that returns a boolean based on its arguments. We prefix the method with the word "is." This is a convention.
isValid
method will return true if the three conditions it evaluates are all true. Otherwise it returns false.public class Program { static boolean isValid(String color, int size) { // Return boolean based on three expressions. return color.length() >= 1 && size >= 10 && size <= 100; } public static void main(String[] args) { // This call returns true. if (isValid("blue", 50)) { System.out.println("A"); } // This call is not valid because the size is too large. if (!isValid("orange", 200)) { System.out.println("B"); } } }A B
While
-loopWe can use a boolean in a while
-loop. Here we continue looping while the boolean is true, and we end the loop by setting the boolean to false.
public class Program { public static void main(String[] args) { boolean value = true; // Continue looping while true. while (value) { System.out.println("OK"); value = false; } System.out.println("DONE"); } }OK DONE
In this program we measure the memory usage of an array of booleans. We allocate one million boolean elements. With freeMemory()
, we determine how much memory is used.
byte
of memory. 16 bytes were used for the array itself.public class Program { public static void main(String[] args) { // Get free memory. long memory1 = Runtime.getRuntime().freeMemory(); // Allocate and use an array of one million booleans. boolean[] array = new boolean[1000000]; array[0] = true; // Get free memory again. long memory2 = Runtime.getRuntime().freeMemory(); array[1] = true; // See how much memory was used. System.out.println(memory1 - memory2); } }1000016
int
We cannot directly convert a boolean to an int
(like 0 or 1). Often a ternary expression is helpful for this requirement.
Sometimes a program checks a set of conditions repeatedly in if
-statements. We can place the result of this check in a boolean.
With booleans, we store true and false values. We store the results of complex expressions. And we can return, and receive, booleans from methods.