Many times, in programming, programmers will need a data type that acts as a switch or flag by storing one of two values, such as:
- YES / NO
- ON / OFF
- TRUE / FALSE
As it happens, Java has just the data type for the occasion: the boolean. This programming tutorial covers the different types of booleans developers will encounter in Java, including the boolean primitive data type, boolean expressions, and the Boolean wrapper object.
Before diving too deep into this guide, you may want to refresh your knowledge on Java primitive data types, as we will be covering Java’s boolean primitive data type first.
The boolean Primitive Data Type in Java
A boolean variable with a lowercase “b” is one of Java’s eight primitive data types. The boolean type can store one bit of data, which indicates the stored value of either true or false.
Here are a couple of code examples demonstrating Java boolean variable declarations that show the convention of prepending an “is” or “are” in front of the variable name:
boolean isUserRegistered = true; boolean areTablesPopulated = false; System.out.println(isUserRegistered); // Outputs true System.out.println( areTablesPopulated); // Outputs false
Flow control structures such as if statements and loops require that their conditions evaluate to a boolean value, so a statement like to following is quite common in Java:
boolean isUserRegistered = true; while (isUserRegistered) { // Do something... if (someCondition) { isUserRegistered = false; } }
Boolean Expressions in Java
Another place that programmers will come across booleans in Java result from logical operators, including:
- | the OR operator
- & the AND operator
- ^ the XOR operator
- ! the NOT operator
- || the short-circuit OR operator
- && the short-circuit AND operator
- == the EQUAL TO operator
- != the NOT EQUAL TO operator
- < the LESS THAN operator
- <= the LESS THAN OR EQUAL TO operator
- > the GREATER THAN operator
- >= the GREATER THAN OR EQUAL TO operator
These Java operators evaluate their operands to return a boolean value of true or false. For that reason, they are employed to evaluate expressions which control decisions in if statements and while loops.
In the below example, we have initialized a couple of boolean and int variables and then applied different kinds of operators to return the values for various conditional checks:
class BooleanOperatorsExample { public static void main(String args[]){ boolean A = true; boolean B = false; int x = 10; int y = 9; System.out.println("A|B = "+(A|B)); // true System.out.println("A&B = "+(A&B)); // false System.out.println("!A = "+(!A)); // false System.out.println("A^B = "+(A^B)); // false System.out.println("(A|B)&A = "+((A|B)&A)); // true System.out.println(""); System.out.println("x > y = "+(x > y)); // true System.out.println("x == 10 = "+(x == 10)); // true System.out.println("x == y = "+(x == y)); // false } } /* prints: A|B = true A&B = false !A = false A^B = true (A|B)&A = true x > y = true x == 10 = true x == y = false */
Here we can see a comparison operator being applied to two int variables in order to determine which block of an if statement to execute:
int myAge = 21; int votingAge = 18; if (myAge >= votingAge) { System.out.println("Old enough to vote!"); } else { System.out.println("Not old enough to vote."); }
You can learn more about logical operators in our programming tutorial: Java Logical Operators.
The Boolean Wrapper Class in Java
All primitive types in Java have a corresponding wrapper class that begins with an uppercase letter; booleans are no exception. The Boolean wrapper class resides in the java.lang package. It wraps a value of the primitive type boolean in an object and contains a single field, whose type is boolean. In addition, this class provides useful methods to convert a boolean to a String and vice versa, as well as for comparing boolean variables.
How to Create a Boolean Object in Java
The Boolean class provides two constructors for creating a Boolean object, either from a boolean or a String:
Boolean b = new Boolean(boolean value); Boolean b = new Boolean(String s);
The constructor that accepts a String creates a Boolean object that contains the value true if the string argument is not null and is equal, ignoring case, to the string “true“, otherwise the Boolean object is assigned a value of false. Here are some examples:
Boolean b = new Boolean(true); Boolean b2 = new Boolean(false); Boolean b3 = new Boolean("true"); Boolean b4 = new Boolean("false"); Boolean b4 = new Boolean("asdf"); // also false
The parseBoolean() Method in Java
Another way to convert a String into a boolean without instanciating a new Boolean object is to use its static parseBoolean() method. Just like the Boolean String constructor, parseBoolean() returns true if its string argument is not null and is equal to the string “true” (again ignoring case), or false otherwise. Here are a few examples:
public class ParseBooleanExample { public static void main(String[] args) { boolean b1 = Boolean.parseBoolean("True"); boolean b2 = Boolean.parseBoolean("TruE"); boolean b3 = Boolean.parseBoolean("False"); boolean b4 = Boolean.parseBoolean("FALSE"); // parseBoolean() does not extract a boolean value from a longer string :-( boolean b5 = Boolean.parseBoolean("That statement is certainly true!"); System.out.println(b1); // true System.out.println(b2); // true System.out.println(b3); // false System.out.println(b4); // false System.out.println(b5); // false } }
You can learn more about the Boolean class in Java’s official documentation here.
Final Thoughts on Java Booleans?
In this programming tutorial, we learned all about booleans in Java, including the boolean primitive data type, boolean expressions, and the Boolean wrapper object. Developers will want to use booleans (and Booleans) any time that you need to work with bit-valued data, that is to say, data which can only have two states. The Boolean object can also store a third state for unknown or unset values as it can contain be Null.
Looking to learn Java in an online course setting? Check out our list of the Top Online Courses for Java Programming.