Home
Map
boolean ExamplesUse the boolean type and the values true and false. Test them with expressions and ifs.
Java
This page was last reviewed on Dec 5, 2022.
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.
Boolean features. It is possible to invert the value of a boolean with a simple expression. Booleans are often useful as local variables.
An example. 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.
Detail The program first assigns the boolean of name "value" to true. The if-statement then detects "value" is true.
Detail We then set the same boolean variable to false. Now the !value test evaluates to true, so the "B" is printed.
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
Flip. 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.
Tip Flipping a boolean can be useful when you want "every other" iteration in a loop to perform a certain action.
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
Expression variable. A boolean variable can store the result of an expression evaluation. It can then be reused, without reevaluating the expression.
Here We see if the size and the color match the conditions for "large and blue." We can then directly test 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 arguments. Methods 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.
Return boolean. 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.
return
Info The 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-loop. We 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.
while
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
Memory usage. 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.
Result The amount of free memory decreases by about 1 million bytes after the boolean array is allocated.
Thus Each boolean element in the array requires one 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
Convert to int. We cannot directly convert a boolean to an int (like 0 or 1). Often a ternary expression is helpful for this requirement.
Convert boolean, int
An optimization. Sometimes a program checks a set of conditions repeatedly in if-statements. We can place the result of this check in a boolean.
An optimization, continued. With the boolean variable, we can test without evaluating the entire expression again. We can just test that boolean value. This simplifies programs.
A summary. With booleans, we store true and false values. We store the results of complex expressions. And we can return, and receive, booleans from methods.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
No updates found for this page.
Home
Changes
© 2007-2024 Sam Allen.