The equality and relational operators in Java are used to determine if value of one operand is greater than, less than, equal to, or not equal to the value of another operand.

These operators return a boolean value determining whether a comparison is true or false. For example **if(5 > 7)** returns false where as **if(5 < 7)** returns true. Equality and relational operators are mostly used in conditional statements like if-else or loops like for-loop to determine when to execute the logic with in the conditional statement and for how many times.

#### Equality and Relational Operators in Java

Operator | Description |
---|---|

== | equal to |

!= | not equal to |

> | greater than |

>= | greater than or equal to |

< | less than |

<= | less than or equal to |

#### Java Example for equality and relational operators

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
public class ComparisonDemo { public static void main(String[] args) { int x = 8; int y = 6; int z = 8; if (x > y){ System.out.println("value of x is greater than y"); } if (x < y){ System.out.println("value of x is less than y"); } if(x == y) System.out.println("value of x is equal to y"); if(x != y) System.out.println("value of x is not equal to y"); if(x >= z){ System.out.println("value of x is greater than or equal to z"); } if(y <= z){ System.out.println("value of y is less than or equal to z"); } } } |

__Output__

1 2 3 4 |
value of x is greater than y value of x is not equal to y value of x is greater than or equal to z value of y is less than or equal to z |

Note that you must use “**==**“, not “**=**” (as “=” is assignment operator), when testing if two primitive values are equal.

That’s all for the topic **Equality And Relational Operators in Java**. If something is missing or you have something to share about the topic please write a comment.

**You may also like**