Although you probably have not heard of Boolean math, you will be happy to know that is an easy concept to grasp. Boolean expressions have a true or false value. In other words, there are only two possible outcomes for a Boolean value.
To declare a Boolean value, we declare the variable type by putting
Boolean in front of the user assigned variable name. Then we use the equals operator (=) and assign the value as true, or false.
In the example below, you can see that we named the variable
bool and set the value to
In order to fully grasp the concept, lets make a little program together!
First, create a class named
BooleanExample and implement the Java main method within that class.
Now, let us declare a Boolean variable named
hasCoffee. If you have coffee, you can set it to true. Otherwise, set it to false.
Now, using the print method, print out the variable
Depending on whether you put true or false, the print method will either print out
Although in this lesson we printed the Boolean value, it is normally used in the background of programs. In future lessons, when we learn about “if-else statements,” you will find how Boolean expressions make up the backbone of Java programs.
Leave a Reply