HomeSearch

Java Boolean Examples: If True, False

Use the boolean type and the values true and false. Test them with expressions and ifs.
Boolean. Day turns to night. Darkness comes over the land. Day and night are abstractions—one is true, the other false. This is a binary state.
With a boolean, we can branch without specifying true or false. By default, an if-statement compares against true. With a negation, we test for false.
This example program uses 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.

True: The program first assigns the boolean of name "value" to true. The if-statement then detects "value" is true.

False: We then set the same boolean variable to false. Now the !value test evaluates to true, so the "B" is printed.

Java program that uses boolean, true, false 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"); } } } Output 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.

Java program that flips boolean value 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); } } Output 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.

Java program that uses expression, boolean variable 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); } } } Output 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.
Java program that uses boolean arguments 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); } } Output 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.

Java program that uses boolean return value 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"); } } } Output A B
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.

Java program that measures boolean memory use 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); } } Output 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 to 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.
With booleans, we store true and false values. We store the results of complex expressions. And we can return, and receive, booleans from methods.
© 2007-2019 Sam Allen. Every person is special and unique. Send bug reports to info@dotnetperls.com.
Home
Dot Net Perls