Scanner class Methods in Java

Java-Relational Operators

Relational Operators

Relational Operators also known as Comparison Operators are used to check the relation between two operands.They are binary operators. In other words, the operators used to make comparison between two operands are called Relational Operators. The result of relation operators is always one value either true or false. They always produce result in boolean type.

For example: We will try to compare two integer values, 5<6 ,"<" is a relation operator to check if 5 is less than 6. If it is less, then the result will be true otherwise it will be false.

Use of Relational Operators

Relational operators are used with conditional and looping statements, where we have to make a comparison  to carry the task.These are used with if statements and loop like for ,while etc to make branching and looping decision.

Types of Relational Operators

There are six types of relational operators used in Java,they are given below:
  
Types of relational Operators in Java


1) Less Than (<) : This operator is used to compare two values and check if the first value is less than second value. We can call values as expression. So , if the first expression lets say exp1 is less than exp2 it will return true as result otherwise the result will be false.
For example: a < b,  if "a" is less than "b" ,it will print true else false.

2) Greater  Than (>) : This operator is used to compare two values and check if the first value is greater than second value. It is exact opposite of less than(<) operator.
For example: a > b,  if "a" is greater  than "b" ,it will print true else false.

3) Less Than or equal to(<=) : This operator is used to compare two values and check if the first value is less than or equal to the second value. It checks two conditions simultaneously. It the exp1 is less than exp2 it will return true but if the exp1 is not less than but equal to the exp2 it will also return true, else it will return false as result. 
For example: a < =b,  if "a" is less than "b" ,it will print true else false. Lets assume "a" is equal to be than it will again return true result.On the other hand, it "a" is greater than "b" the result will be false.

4) Greater than or equal to(>=) : It is just like the less than or equal to operator but it works in opposite direction.It checks is the first exp is greater than or equal to second expression.
For example: a >= b,  if "a" is greater than or equal "b" ,it will print true else false.

5) Equal to(==) : This operator is used to compare two values and check if the first value is equal to the second value. This is not that ordinary equal to "=" of math, it is represented by "==" sign. It return true if the exp1 is equal to exp2 else it will return false.
For example: a == b,  if "a" is equal to  "b" ,it will print true else false.

6) Not Equal to(!=) : This operator is used to compare two values and check if the first value is not  equal to the second value. It is opposite of equal to (==) operator. It returns true is the exp1 is not equals to exp2.
For example: a != b,  if "a" is not equal to  "b" ,it will print true else false.

Try to understand these operators, they are extremely useful in programming.Just learn with patience and don.t get confuse.

Example:  This example will illustrate all the relational operators.Write the code as below in your IDE or notepad.
Types of relational Operators example in Java

Output:

Types of relational Operators example in Java


Experiment: Alter the values of a & b. Find out the result of each statement by yourself first then compare your output with console output.

Happy Coding!

Comments