Curriculum
Course: Learn Java Programming
Login

Curriculum

Learn Java Programming

Text lesson

Relational Operators in Java

In this lesson, you will learn.

  • Relational Operators in Java
  • Examples

 

Relational Operators in Java

Relational operators in Java are used to compare two values and determine the relationship between them.

These operators return a boolean result, indicating whether the specified relationship holds true or false.

The common relational operators in Java include.

 

1. Equal to (==):

Checks if two values are equal.

int a = 5;
int b = 7;
boolean isEqual = (a == b); // false

2. Not equal to (!=):

Check if the two values are not equal.

int x = 10;
int y = 10;
boolean isNotEqual = (x != y); // false

3. Greater than (>):

Checks if the left operand is greater than the right operand.

double p = 3.14;
double q = 2.71;
boolean isGreater = (p > q); // true

4. Less than (<):

Checks if the left operand is less than the right operand.

int m = 15;
int n = 20;
boolean isLess = (m < n); // true

5. Greater than or equal to (>=)

Checks if the left operand is greater than or equal to the right operand.

int num1 = 8;
int num2 = 8;
boolean isGreaterOrEqual = (num1 >= num2); // true

6. Less than or equal to (<=):

Check if the two values are not equal.

int alpha = 5;
int beta = 7;
boolean isLessOrEqual = (alpha <= beta); // true

 

Summary of Relational Operators

Operator Description Example Result e.g. x=7, y=3
== Equal to x == y false
!= Not equal to x != y false
> Greater than x > y true
< Less than x < y false
>= Greater than or equal to x >= y true
<= Less than or equal to x <= y false

 

Relational operators are frequently used in control flow statements, such as if statements and loops, to make decisions based on the relationships between values.

They play a crucial role in programming logic and comparisons.

 


End of the lesson….enjoy learning

 

Student Ratings and Reviews

 

There are no reviews yet. Be the first one to write one.

 

 

Submit a Review