# Relational and Logical Operators in Java

\$\$/\$\$

In this video, we will discuss boolean variables in detail — a topic we introduced earlier as well. You already know that these variables can have true or false values only. But you’re still not clear about the situations where you can apply them or use them. You will learn about this in the following video.

\$\$/\$\$

## Video Transcript

So now we are going to take a look at another data type in Java which are known as Booleans. Then further ahead we are also going to discuss some other stuff such as relational operators and logical operators. So for now, recall that Boolean is a data type into which you can only store two types of values. That is a Boolean variable can only be either true or it can be false. The size of the Boolean variable is exactly one bit. So for example, I can declare a Boolean like this. I'll say boolean A is equal to true. Similarly, I can declare another variable, say boolean B is equal to false. So both of these Booleans contain the value true and false respectively. In what cases will these Booleans or the Boolean data type be useful? Well, they can be highly useful in some cases in which we have to compare two quantities. That is, if I have to check if two quantities are equal or if two quantities are unequal, or if one of the quantities is greater than or less than the other quantity, then Boolean variables are really very useful. For example, if I wish to know whether four is greater than equal to five, then in that case I can store the result of this particular mathematical operation into some particular Boolean variable. So these kind of relations or these kind of symbols which are used to compare two different entities are known as relational operators.

So some of the commonly used relational operators are greater than or less than, greater than, equal to, less than equal to, equal to, equal to or not equal to. So let us walk through each one of them and see what results we get. So I say that Boolean x one is equal to. I say suppose five is greater than equal to nine and I store the result of this particular operation inside my Boolean x one. Now, when I try to print the value of x one, what do you think will be printed? Since five greater than equal to nine is a false relation because five is actually lesser than nine. So this x one will contain the value false inside it and false will be printed on the console. Similarly, if I say Boolean x two is equal to, say five is equal to is equal to seven, then what do you think will be printed when I try to print x two? Well, since this relationship that is five is equal to, is equal to seven is actually false because five is actually not equal to seven. So the value false will be contained and stored inside the variable x two. So when I try to print X two it is going to print false here. Now, if I wish to compare two quantities and check if they are unequal, then I can use the not equal to operator. The not equal to operator is comprised of an exclamation mark and an equal to symbol. So I say that Boolean x three is equal to five, not equal to seven. Now, when the program encounters this statement, it sees five not equal to seven. So it checks if this statement is true or false, since five is actually not equal to seven. So this particular relationship evaluates to true. So when I try to print x three, it will print true on the console. Similarly, what do you think will be printed when I try to print four greater than five? When I try to print four greater than five, it is going to print false. Here, if I try to print two less than seven, what do you think will be printed? Well, since this relationship is true, because two is actually less than seven. So here true will be printed.

So basically, Booleans are very useful in comparing any two quantities of which initial value. Also, you might not know here it is easy to see that five is greater than equal to nine. But perhaps there might be cases in which we need to compare two variables of which whose value we do not know. For example, I might have some number stored inside my variable A and another number stored inside variable B. Then based on that, I would need to perhaps check whether A is equal to equal to B. In that case, I will write A equal to equal to B, and the result of this particular relational operation will be stored inside another Boolean variable. Then, based on the result of whether A is equal to B, it will either print true or false. So this is how relational operators and Booleans work.

## Video Recap

• Booleans are a data type in Java that can store only true or false values, with a size of one bit.

• They are useful in comparing two quantities using relational operators such as greater than or less than, greater than or equal to, less than or equal to, equal to, and not equal to.

• Relational operators can be used to compare two variables, and the result is stored in another Boolean variable.

• Booleans are helpful when comparing two variables of unknown values.

• Relational operations can be stored in a Boolean variable and printed as true or false based on the comparison.

In the previous video, you learned about relational operators.

You just saw the kind of situations in which you use the boolean data type. You learnt about the operators (>, >=, <, <= , ==, !=) that are used to compare true values and to frame a condition that a boolean data type evaluates to true or false.

Let’s now learn about logical operators, which make boolean data types even more useful.

\$\$/\$\$

You learned about the “and” and “or” operations here. The “and” operation results in a true value only both of the conditions involved are true. Whereas, the “or” operation results in a true value even if one of the conditions involved is true.