Logical Operators In Java

Logical Operators in Java with example

As the name suggests, these operators are used to perform logical operations on operands. You would see these operators more often being used to evaluate multiple conditions together. These are often used with the boolean data type and they also return a boolean value as the result.

Let’s consider a couple of examples, of which one is a real-life, and another one is an example implementation of program logic.

Example 1: Let’s consider that you go to buy some clothes at a cloth shop. There is an offer running, that if you buy 2 jeans and 1 shirt, you get a free t-shirt. Now, the offer is only applicable if you buy 2 pairs of jeans AND 1 shirt. Notice the AND here. It is not like you buy 2 pairs of jeans and you are eligible for the offer. You must buy 2 pairs of jeans and 1 shirt only to be eligible for the offer. Now, here we are evaluating multiple conditions like checking whether or not the user has bought exactly 2 pairs of jeans and 1 shirt. If the condition is true, then only he/she is eligible to get a free t-shirt, not for any other purchase.

Example 2: Here is a bit technical example so you all can relate the use of these operators in programming, because there may not be any need to write a program to check whether or not someone has bought 2 jeans and 1 shirt, So, let’s go a bit technical here. Let’s suppose you want to check whether a number is divisible by both 3 and 7.

In this case, you are checking multiple conditions, right? We are checking whether or not some number is divisible by 3, and also by 7, and if the resultant condition is true(which means that if the number is divisible by both 3 and 7), then we are supposed to do something, and else, we are supposed to do some another thing. So, one easy approach would be using the AND logical operator. Let’s see what are the different logical operators –

The three logical operators are –

  • AND → represented as && when we write programs…
  • OR → represented as || when we write programs…
  • NOT → represented as! When we write programs…

now, when we have seen the different logical operators, let’s have a brief talk about them one by one.

&& → Hola… I am the logical AND operator. I can help you in evaluating multiple conditions since I am a logical operator… But the thing is, if you want me to give a true output, then I want every condition that you have to be true! If you did not understand? Let’s have an example… let’s consider that it is raining outside, and you want to go out for some work. Now, you think that – “ok… if it is raining outside, AND I want to go outside, then I will wear a raincoat…”. So, just understand that ultimately we are deciding whether or not we are going to wear a raincoat.

Now, just imagine that you don’t want to go outside, but it is raining outside, so are you going to sit in your home wearing a raincoat? NO right? Well, you can if you want but the obvious thing is that you won’t wear a raincoat unless you need to go out in rain. Let’s write the above-quoted sentence separately below just for the sake of explanation –

If it is raining outside AND I want to go outside, then wear a raincoat.

Now, split the above sentence, which makes the following sentences –

  • It is raining outside. → this is one condition (this can be true or false, meaning raining or not raining in this case)
  • I want to go outside. → this is another condition (this can also be true or false, which means either go or don’t go)
  • Wear a raincoat. → this is some outcome. (or something you would want to do)

Now, the third sentence here is the outcome, and in the first two sentences we are checking so that we can decide whether or not the third thing should happen. Now, let’s consider the following 4 scenarios. But before we move any further, just remember that for the output to be true in this case, all the conditions should hold true –

  • It is raining outside AND I don’t want to go outside.
    Now, the thing is that the first sentence is true, but the second one is not. So, logically, if it is raining outside but you don’t want to go outside, then why are you even going to wear a raincoat inside your house right? So, this is like true && false, which evaluates to false.
  • It is not raining outside AND I want to go outside.
    In this scenario, all the things are clear. If it is not raining outside, and you want to go outside, again you don’t need to wear a raincoat. You can just go outside. Here also, some condition is false, which is why the result is false. (Here, the result is false means that you don’t have to wear a raincoat, and the true result means that you have to wear a raincoat). So. Here, false && true also evaluates to false. Now you can see that if any condition is false, then the result is going to be false.
  • It is not raining outside AND I don’t want to go outside.
    In this scenario, it is not raining outside, and you don’t want to go outside either. So, there is no scope to wear a raincoat right? Unless if you think something like – “Hey… how about wearing a raincoat today. I haven’t worn my raincoat for long?” So, here, both the conditions are false, and again we have the result as false. So, false && false evaluates to false.
  • It is raining outside AND I want to go outside.
    Well, in this scenario, both the conditions are true. It is raining outside, and you want to go outside. So, here it is logical that you would suggest yourself putting on a raincoat. So here, since both the conditions are true, the result also evaluates to true, which means that you should wear a raincoat. So, true && true evaluates to true. Let’s wrap up this thing using a table.
Value 1Value 2Outcome
FALSEFALSEFALSE
FALSETRUEFALSE
TRUEFALSEFALSE
TRUETRUETRUE

Values and outcome for logical &&

Note that this thing is not only limited to two conditions. You can have many conditions there as per your requirement. You can do something like this –

condition1 && condition2 && condition3 … && conditionN

In the above expression, N is possibly some number. This was just to show that you can have multiple conditions here. You can even take these expressions to a more complex level. Still here also, we need all the conditions to be true if we want to have a true output. Any condition which is false can make the whole result false.

  • || → Hey… I am the logical OR operator. Well, if you are using me as a logical operator, I can give you a true output even if any one condition is true. Let me introduce myself with an example. Just imagine that you take your umbrella with you when you go outside, if either it is raining outside, OR there is too much heat outside. Now, read that sentence again. It is just telling you that if any one of the conditions is true, you are going to take an umbrella with you. Even if both the conditions are true. Well, here also, as an example, we are considering two conditions, but you can have as many conditions as you need. Maybe you just like to take your umbrella with you wherever you go. So, that can also hold to be a condition. But the important thing here is that if any condition separated by the OR operator evaluates to true, the result is evaluated to true. Let’s break the above sentence just like the previous operator so that we can better understand things. So, here are the sentences after the breakdown –
  • is it raining outside? (this is a condition. It is supposed to be either true or false)
  • It is too hot outside. (this is also a condition. It is also supposed to be either true or false)
  • Carry an umbrella. (this is something that is supposed to happen if the result evaluates to be true)
    Now, let’s consider some scenarios that may arise in this situation –
  • It is not raining outside OR it is too hot outside.
    Now, in this scenario, it is not raining outside, this is clear, but it is too hot outside. It suits here that is ok… it is not raining, but it is hot outside, so we should take our umbrella with us so that we can be safe in the heat. So, even if one condition evaluates to be true, the result is going to be true. (in this example, the true result means that you are going to take an umbrella with you, and the false result means that you are not going to take your umbrella with you)
  • It is raining outside OR it is not hot outside.
    In this situation too, the thing is that even if it is not hot outside, it is raining, so it is clear here that we are going to take our umbrella with us. This is so simple, right? Just at least one condition has to evaluate to be true, and your result is true.
  • It is raining outside OR it is hot outside.
    Again this is clear, that either of the conditions can be true, like either there is raining outside, or it is hot outside, or there might be both cases together. So, we got to take our umbrella with us anyways. So, the result, in this case, is also true
  • It is not raining outside OR it is not hot outside.
    Well, this is very straightforward to understand right? It is not raining outside, or it is not hot outside, so both the conditions support that ok… you don’t need to take your umbrella with you.

Again here as well, we can have multiple conditions, separated by the logical OR operator.

  • ! → Hey… This is a logical NOT operator. This looks like an exclamation mark. What does it do? It simply reverses the logical state of some operand. This is like if something is true, then it reverses to false. This is pretty much easy and straightforward. Something that is true is going to be false when reversed by this operator, and on the other hand, something that is false is going to be true when reversed by this operator.

This is like! true is false, and! false is true. You can read it as “NOT true is false”, and “NOT false is true”.

We are going to make use of the logical operators ahead, so you don’t have to worry about the implementation right now, but just remember what are they used for.