Relational Operators in Java with Example

Operators are special symbols that perform specific operations on one, two, or three operands, and then return a resultThere are different types of operators are available in java which are given below:
1. Arithmetic Operators.
2. Unary Operators.
3. Assignment Operators.
4. Relational Operators.
5. Logical Operators.
6. BitWise Operators.
7. Ternary Operators.
8. InstanceOf Operators.

Relational Operators in Java:

In the last tutorial, we have learned about arithmetic operators, unary operators, and assignment operators. We have also learned the example of these operators. In this tutorial, we will learn what is relational operators in javaThe Relational Operators are used to check the relations between the two operands. Relational operators are also called comparison operators because it is used to make a comparison between the two operands. The result of relational operators is always a boolean value. The relational operators are used in the if-else statement and the various loop statements. The following table shows you different relational operators used in Java.

1. Equal to(==) operator: This operator is used to check whether the two operands are equal or not. It returns true if both the operands are equal.

2. Not Equal to(!=) operator: This operator is used to check whether the two operands are equal or not. It returns true if the left-hand side operand is not equal to the right-hand side operand, otherwise, it returns false.



3. >(Greater than) operator: This operator is used to check the first operand is greater than the second operand or not. It returns true if the first operand is greater than the second operand.

4. >=(Greater than equal to) operator: This operator is used to check whether the first operand is greater than or equal to the second operand or not. It returns true if the first operand is greater than or equal to the second operand.

5. <(less than) operator: This operator is used to check the first operand is less than the second operand or not. It returns true if the first operand is less than the second operand.

6. <=(less than equal to): This operator is used to check whether the first operand is less than or equal to the second operand or not. It returns true if the first operand is less than or equal to the second operand.

Example of Relational Operators in Java:


class RelationalOperator{

    public static void main(String[] args){
        int value1 = 10;
        int value2 = 20;
       
        System.out.println("value1 == value2 --> " +(value1 == value2));
		System.out.println("value1 != value2 --> " +(value1 != value2));
		System.out.println("value1 > value2 --> " +(value1 > value2));
		System.out.println("value1 >= value2 --> " +(value1 >= value2));
		System.out.println("value1 < value2 --> " +(value1 < value2));
		System.out.println("value1 <= value2 --> " +(value1 <= value2));
    }
}


Output:
Java Relational operators example


You may also like these related posts:

1. Java Assignment Operators with example.
2. Java Unary Operators with example.
3. Java Arithmetic Operators with example.
4. Basic data types in java.
5. Java variables complete tutorial.
6. Java Hello World program - Java first program
7. How to set path in java - java environment set up.
8. Top 100 java interview question and answer.

Relational Operators in Java with Example Relational Operators in Java with Example Reviewed by Prashant Srivastava on December 15, 2019 Rating: 5

No comments:

Powered by Blogger.