Java: Logical Operators

Instructor: Martin Gibbs

Martin has 16 years experience in Human Resources Information Systems and has a PhD in Information Technology Management. He is an adjunct professor of computer science and computer programming.

Logical statements are as integral to programming as there are in everyday life. They give us an enormous amount of power in our programs. This lesson will cover these statements in the context of Java and provide working code examples.

Logic

We use logical statements every day. For example, if I can buy bread AND tuna, I can proceed to make a tuna sandwich. To a computer, this statement is easy, because the result is either yes/no, or true/false. It's the same for us. If we didn't get the tuna, the result of the statement is a no or a false, and we're not going to be making a tuna sandwich!

In programming we refer to these logical operators as AND, OR, and NOT. These statements are represented in Java by symbols:


Java logical operators list


AND

When we want to check if two or more conditions are true, we use the AND operator. In the case of our sandwich making, it only makes to make a sandwich if you have both tuna AND bread. Java won't continue processing until ALL conditions are true.

Take a look at the code example below, which shows this operator in action:

boolean sandwich;
boolean haveBread;
boolean haveTuna;
if(haveBread == true && haveTuna == true) {
  sandwich = true;
}

OR

Sometimes a tuna sandwich is good with either onions OR celery. In that case, we can use the OR operator to check if either is true. Let's look at the code. In this case, celery is true and onion is false. But since we use an OR statement, the result of the sandwich test will be true.

boolean celery = true;
boolean onion = false;
if(celery == true || onion == true) {
  sandwich = true;
} else {
  sandwhich = false;
}
System.out.println(sandwich);

The output:


Java logical OR output


It's true because one of the values was true.

NOT

The NOT operator is helpful when you want to see if one or more conditions are NOT true. For example, I'll make the tuna sandwich if its NOT winter or I'll make it if it is NOT Monday and the sun is NOT shining. You may wonder why you wouldn't just build the statement with ANDs and ORs.

The Java keyword for NOT is an exclamation point (!). You can put this in front of the entire set of things you are testing to tell Java to process the code only if none of that is true. This is incredibly powerful! You could have a huge set of tests.

To make sure it works either way, all you have to do is add or remove a single exclamation point! You don't have to mess with the ANDs and ORs or move code around to test it. Just add or delete the exclamation point.

Here is a very simple NOT statement that checks if it's winter. If it isn't winter, we won't make the sandwich.

boolean sandwich;
boolean winter;
if (!winter) {
  sandwich = true;
} else {
  sandwich = false;
}

Exclusive OR

There is one more logical operator, the exclusive OR (XOR). It can be a confusing concept at first.

You may say that a truck is either red OR it's blue. Let's say you are comparing two trucks. You could say if truck A is blue OR truck B is blue, then let them through the toll gate. But what if BOTH are blue? Technically this passes the test of an OR logical operator, but in reality it's more of an AND statement, since both trucks pass the test.

Think of it with regard to taxes. You can have one type of deduction or another, but not both. The XOR operator works the same way.

The operator in Java for the exclusive OR is the caret (^).

In keeping with our tuna sandwich example, let's look at how this plays out. Remember in the OR statement we said that either celery or onions were fine. But we don't want both. That's too much root vegetable for our tastes.

In the following code we've set both variables to true. Now, instead of the standard OR, we'll use the Exclusive OR (XOR):

To unlock this lesson you must be a Study.com Member.
Create your account

Register to view this lesson

Are you a student or a teacher?

Unlock Your Education

See for yourself why 30 million people use Study.com

Become a Study.com member and start learning now.
Become a Member  Back
What teachers are saying about Study.com
Try it risk-free for 30 days

Earning College Credit

Did you know… We have over 200 college courses that prepare you to earn credit by exam that is accepted by over 1,500 colleges and universities. You can test out of the first two years of college and save thousands off your degree. Anyone can earn credit-by-exam regardless of age or education level.

To learn more, visit our Earning Credit Page

Transferring credit to the school of your choice

Not sure what college you want to attend yet? Study.com has thousands of articles about every imaginable degree, area of study and career path that can help you find the school that's right for you.

Create an account to start this course today
Try it risk-free for 30 days!
Create an account
Support