In one of our previous posts, we discussed the many different data types in Java. Just to refresh your memory, data types are basically a way to categorize the kind of data a variable is holding. As an example, suppose a variable named ‘x’ holds an integer value of ‘15’. In such a case, the variable would belong to the ‘int’ data type.
Boolean is another data type in Java that helps add logic to a program. In this blog post, we’ll learn more about the boolean data type and how it is used in Java. For a more in-depth explanation of the boolean data type, consider taking a course on programming in Java for beginners
What is Boolean Data Type?
As mentioned above, the boolean data type adds logic to a program, i.e. it helps us make decisions. The best way to illustrate this is with an example from the real world:
Let’s say that you’re driving to the gym. You come upon an intersection and you know that turning left will take you to the gym, turning right to the movie theater, and going straight to the restaurant. You know have a logical decision to make, which can be encapsulated in three questions:
If I turn left, will it bring me to the gym?
If I go straight, will it bring me to the gym?
If I turn right, will it bring me to the gym?
There are only two answers to each of these questions, yes or no. It’s basically the same in Java, where Booleans will tell the program which is the best course of action to take. In Java’s case however, the values for the Boolean keyword are true and false, instead of yes and no.
But What if you were driving a car with a GPS system installed? Let’s just say this GPS system runs on Java (which it actually does in most cases). Before you left home, you asked the GPS system to take you to the gym. What simple code could we create that will help the robot decide which way to turn? Let’s start.
Of course, this is a simplification of the entire process – the real program wouldn’t be written in this manner – but it should give you a fair idea of how boolean data works.
Now based on this simple code, it will go to the direction (variable) in which the primitive type is true. If not true, it will simply ignore that variable. This is just the very basics of boolean logic in Java
Confused? Learn more in this course on Java for absolute beginners.
Boolean Logic and IF Statements
You’ll use boolean data types a lot with ‘if statements’. For the uninitiated, if statements help make decisions – if X is true, perform action A, else perform action B. The syntax is:
if (object==value){pursue action}
Where the opening curly bracket can be taken to mean ‘then’. This is like saying, “if the condition holds true, pursue the specified course of action; if the condition is false, ignore the action, or perform another action altogether”.
So let’s understand this with the example we used earlier:
Now this code makes sense, and shows you how logic comes into computer programs. Feel free to put this code into your text editor or IDE and see what happens! Don’t worry too much about if statements, we’ll discuss them in depth later on.
Learn Java from scratch in this course.
Boolean Logic with Operators
We can combine two or more Boolean expressions into a single Boolean expression by the use of four types of operators: and, or, exclusive or and not.
The ‘and’ Operator
This combined expression is true only if BOTH parts of the expression are true.
Let’s look at the following statement:
It is overcast outside and y is equal to 8.
We can divide these into two statements: one before the ‘and’ and one after it. Now, both these statements can either be true or false. Let’s say for this case, that it is indeed raining outside and y is equal to 8. Since both these statements are true, then the expression on a whole is true.
How about if it was sunny outside and y is equal to 6, not 8. In this case, since both these statements are false, the entire expression becomes false.
Now, what if it is raining outside but y is not equal to 8? In this case, since one of the conditions is false, the entire expression becomes false. Remember, BOTH statements have to be true in order for the entire expression to be true.
This can be expressed in the following tabular form:
AND | True? |
True and False | False |
True and True | True |
False and True | False |
False and False | False |
The and operator is denoted by the symbol &. There is another symbol, &&, which is called “short-circuit and”. This will short-circuit the expression and the second part will not be checked.
The ‘or’ Operator
This combined expression is true if EITHER part is true.
Let’s look at the following statement:
Today is Monday or fish ride bicycles.
Again, let’s make like a compiler and divide this expression into two. Let’s say today is indeed Monday, but we all know that fish do not ride bicycles. In this expression only one statement is true, but the whole expression turns out to be true. Why? Because only one part of the expression needs to be true for it to be true on a whole.
What if someone did manage to teach fish to ride bicycles and today is indeed Monday? Then the whole expression will be true as both conditions are true.
But what if today is not Monday and fish still can’t ride bicycles? Then the entire expression is false, since neither statements are true.
This can be represented by the following table:
OR | True? |
True or True | True |
True or False | True |
False or False | False |
False or True | True |
This operator is denoted by the symbol |. There is another symbol, ||, which is “short-circuit or”. It will short-circuit if the first part of the expression is true. So if the first part is true, it doesn’t matter what the second part is, the whole expression will be true.
The ‘exclusive or’ Operator
This combined expression is true only if ONE part is true and the other part is false. So let’s go back to the example we used above. If today is Monday, then the expression will be true (considering fish do not ride bicycles). If today is not Monday however, then the entire expression will be false. However, if today is Monday and scientists found a way to grow legs on fish, then the entire statement is still false. Remember, only ONE part must be true. This is the only case where two rights make a wrong. This operator is denoted by the symbol ^.
The ‘not’ Operator
This negates a Boolean expression. So basically, if a Boolean expression is false, then adding a not before the expression will make it true, and vice versa. Let’s look at this example:
Not (fish swim on land).
The structure of this statement may look grammatically incorrect, but it is indeed correct in programming syntax. If the expression in the brackets is true, then the ‘not’ will make it false. This operator is denoted by the symbol !.
Understanding boolean logic is essential to understanding Java, and indeed, programming. Brush up your knowledge of boolean logic and more complex concepts in this course on advanced Java.