This is our third lesson on operators. In this lesson we are going to study about logical operators, relational operators and and the conditional operator.
Logical Operators:
Logical operators are applied on Boolean values. Remember that Boolean value is either true or false. We studied about Boolean variables in the variables lesson. Following are the logical operators available in Java.
Logical AND operator:
Logical AND operator is represented by &&. The AND operator has two values as an Input and one value as an output. All three of the input and output values are Boolean i.e they are either true or false. The AND operator returns true only when both of its inputs are true otherwise it returns false. The truth table for AND operation is shown below.
a

b

a && b

TRUE

TRUE

TRUE

TRUE

FALSE

FALSE

FALSE

TRUE

FALSE

FALSE

FALSE

FALSE

Truth Table: Truth table is a table that shows the outputs for all the possible inputs of a logical statement.
Logical OR operator:
Logical OR operator is represented by . The OR operator has two values as input and one value as output. All three of the input and output values are Boolean i.e. they are either true or false. The or operator returns true when either of the two input values are true otherwise it returns false. The truth table for OR operation is shown below.
a

b

a  b

TRUE

TRUE

TRUE

TRUE

FALSE

TRUE

FALSE

TRUE

TRUE

FALSE

FALSE

FALSE

The OR operator is represented by the double vertical bar which is located above the enter key in your keyboard. (Use shift key to access it)
Logical NOT operator:
Logical NOT operator is represented by !. It takes only one value as input and returns one value as output. It reverses the value of a Boolean variable. For example, it turns true to false and false to true. The truth table for NOT is shown below.
a

!a

TRUE

FALSE

FALSE

TRUE

Let’s take an example on logical operators.
Relational Operators:
The next concept is of relational operators. The relational operators justify the relation between two quantities. They take two values as input and return only one Boolean value as an output. This means that their output is either true or false. We have following relational operators in Java.
Greater than ( > ):
It takes two values as input and checks if the first value is greater than the second value. If so, it returns true, otherwise it returns false. For example, 20>10 will return true whereas 30>500 will return false.
Less than ( < ):
It takes two values as input and checks if the first one is less than the second one. If so, it returns true otherwise it returns false. For example, 10<10 will return false whereas 20<60 will return true.
Equals ( == ):
The Equals operator is presented by two equal signs. It is different from the assignment operator which is represented by only one equal sign. It checks if the first value is equal to the second value. If so, it returns true otherwise it returns false. For example, 10==20 will return false whereas 10==(5+5) will return true.
Greater than or equal to ( >= ):
It takes two values as input and checks if the first one is greater than or equal to the second one. If so, it returns true otherwise it returns false. For example, 10>=10 will return true. 10>=5 will also return true. 20>=30 will return false.
Less than or equal to ( <= ):
It takes two values as input and checks if the first one is less than or equal to the second one. If so, it returns true otherwise it returns false. For example, 20<=20 will return true. 30<=20 will return false. 30<=100 will return true.
Not equal to ( != ):
The not equal to operator is represented by an exclamation mark followed by an equal sign. This is the opposite of the equals operator. It will return true if the both values at its input are not equal otherwise it will return false. For example, 10!=10 will return false whereas 20!=30 will return true.
Let’s take an example on relational operators.
The last operator of this lesson is the:
Conditional operator ( ?: ):
The conditional operator is also known as ternary operator. It is called ternary operator because it takes three values as input and returns one value as output. Its first input value is a Boolean variable or a Boolean expression and the next two are any values (but of the same data type). Based on the value of Boolean variable or Boolean expression it decides which of the two values (which were gives as input) to give as output. This operator is actually used to make a decision. This operator is used in the following format:
anyVariable = (aBooleanExpression) ? valueIfTrue : valueIfFalse ;
This is understood better by examples.
Let’s see its output.
As the expression 30>40 was false, so the second value (i.e. 40) was given as output.
Let’s take another example. A person can caste a vote if he or she is above 18 and has a national ID card.
In the above example. we also used the logical operator &&
Let’s take another example. A student can sit in the examination only if he has either the CNIC or BForm with him.
Operator Precedence in Java:
In smaller classes, you learnt about DMAS rule. Remember? What was that? It was meant to teach you that which operations should be performed first. According to it you first performed division, then multiplication, then addition and then subtraction. In the same way operators in Java have their sequence also. The sequence in which you should perform the actions of the operators is shown below:
1. Brackets
2. Increment and decrement operators (++,–)
3. Logical NOT operator
4. Division
5. Multiplication
6. Modulus (%)
7. Subtraction
8. Addition
9. Equal to (==)
10. Not equal to (!=)
11. Logical AND (&&)
12. Logical OR ()
13. Ternary Operator (?:)
14. All assignment operators
For more information on operator precedence,
see this
Post Views:
1,023