Logical Operators


Every programming language has its own logical operators, or at least a way of expressing logic. Java's logical operators are split into two subtypes, relational and conditional. You can use these operators to make your programs much more flexible and powerful. You'll also get the added benefit of making your code even that much easier to read and to write.

Relational Operators

You should have seen these operators before if you've been following these tutorials. I am going to complete the list, so you will have all of Java's relational operators in front of you.

Conditional symbols and their meanings
== is equal to
!= is not equal to
> is greater than
< is less than
>= is greater than or equal to
<= is less than or equal to

Relational operators are used primarily in if statements, while loops, and on occasion for loops. There should not be any other cases of their usage.

The only addition I made from the previous list is the "is not equal to" symbol. Here is how it looks like in an if statement:

Now the if statement is true when the variable num is not equal to 3. Otherwise, the condition is true.

Because you have seen the rest of the relational operators, I will not go over them here. You can check out how they are used in the conditionals tutorial found in the Java for Beginners section of the site.

Conditional Operators

There are four logical operators in Java, however one of them is less commonly used and I will not discuss here. It's really optional compared to the ones I will introduce, and these will be a whole heck of a lot useful to you now.

Here's your table of logical operators:

Conditional symbols and their meanings
&& AND
|| OR

Conditional operators, like their relational counterparts, are also mostly used in if statements, while loops, and for loops. The best way to show how effective these are is to just show you how they make your code look a lot cleaner.

And Operator

Let's say you wanted your program to output "You Win!" when num1 is equal to 3 and num2 is equal to 5.

First, Java will look at the first if statement. Then, if it's true, it will go on to the second if statement. If that one is also true, then it will print out "You Win!" on the screen. Seems simple enough.

But there has to be a better way than putting an if statement inside of another if statement!

There is. Enter the AND operator.

Do you see how much cleaner that code looks? It even sounds better when you say it in English. If num1 is equal to 3 and num2 is equal to 5, then output "You win!". This is much, much better.

Or Operator

Up until this point, it was ridiculously difficult to make a bunch of if statements so that the program will execute a piece of code if only one out of several conditions is true. The OR operator allows just this, the ability to have an if statement be true if only one out of several conditions is met.

Do you see how this example of code is different from the previous? The code will run if num1 is equal to 3 OR num2 is equal to 5. Only one of them has to be true for that to happen. If both are true, that's fine also. The only time that if statement will be false is when num1 is not equal to 3 AND num2 is not equal to 5.

Learning about the logical operators means that you too will need to brush up on your logic. I think the easiest way to put it is like this:

AND is when you need everything to be true

OR is when you need only one thing to be true

That's really all there is to it.

There is a common mistake I ALWAYS see when people use the OR operator:

The editor I use, Eclipse, will show this line as an error. Do you understand why?

Those just starting out believe that through clever use of parenthesis, they can make that mean that num1 can be equal to 3 or 5. This however is wrong. Do not try to shorten your logic statements in this way, because Java doesn't understand what it means. Even though it looks like it should work, it doesn't. Remember Java always executes code inside parenthesis first! In this case it looks inside the parenthesis, sees 3 or 5, and has no idea what that means. Do not fall for this trap and make sure you always use the OR operator in the proper way.

Not Operator

The not operator is probably the easiest to understand. It is simply the opposite of what the condition says.

Notice how I needed to an extra set of parenthesis. Basically the if statement says if NOT num1 is equal to 3. In other words, if num1 is not equal to 3. Do you see how that works? I would definitely try it out on your own so you can see the results. If you notice, the statement "is not equal to" looks awfully familiar. Wait a second...

This is the exact same thing. You'll notice this is an easier way to use "is not equal to". In general, you should be using this other form and not the previous one as this is easier to read.

Here's a much better example of using the not operator on its own.

Ok ok let's back up. First of all, I'm using a boolean variable. I created one and set it to true. Next I use an if statement. In English it reads, "if not flag". Remember that flag is equal to true, so the English becomes, "if not true". So how does this work?

"if not true" is asking if the variable flag is not true, otherwise known as false. If flag is false, Java will display "You win!". Flag is set to true, so that code will not execute. If you're following along (which I HIGHLY recommend), then try taking out the NOT operator and running the code to see that Java will display "You win!" now that the if statement is checking if flag is true.

Order of Logical Operators

Logical operators also have an order of operations. This is incredibly important when you start to mix and match operators. The AND operator always happens before OR operators. In Java, only parenthesis can give them a different order. Let's look at the following example:

What do you think will happen? Will Java display "You win!"? Let's read the logic. If num1 is equal to 9 and num2 is equal to 2 or num3 is equal to 7. So supposedly you might say ok, since num1 is not equal to 9, then the rest of the statement does not matter, and nothing will be displayed.


Java reads that statement strictly from left to right. Here is what really happens.

Java looks at the first two conditions. If num1 is equal to 9 or if num2 is equal to 2. Then, it determines that neither of those are true. Then, it looks at the OR operator. Java then says ok, is the other side of the OR operator true? Since num3 is indeed equal to 7, “You win!” will be displayed. It took the first two conditions, combined them, and then compared that to the other side of the OR operator.

To get the previous result, we would need to wrap the OR part in parenthesis like this:

Now nothing will be displayed.

Until you get comfortable with all of these logic operators and the logic behind them, you should try using as many parenthesis as possible. This will make sure you do not get confused as to how the logic is grouped.

I hope that this run down of the different logical operators makes sense, and that you can start applying them to your programs right away. They are incredibly useful and will allow you to make much more sophisticated programs. Try coming up with different experiments using most of the logical operators so that you get a better feel for handling them. If you have any interesting logical scenarios you'd like to share or if you have any questions, feel free to use the questions/comments form found at the bottom of this tutorial.

In the next tutorial we're finally going to delve into what makes Java tick, the object-oriented side of Java! I've been avoiding discussing Java objects until now, but I think it may be time to shed some light on the topic.

If you have any questions, comments, or concerns about this tutorial or logical operators, feel free to contact us.

Custom Search