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
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
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 Java Operators: Equality And Relational. If something is missing or you have something to share about the topic please write a comment.
You may also like
- Java Operators: Assignment, Arithmetic And Unary
- Java instanceof Operator With Examples
- Java throw Keyword With Examples
- ArrayList Internal Implementation in Java
- Thread Group in Java
- Check Given String Palindrome or Not in Java
- Hadoop MapReduce Word Count Program
- Abstract Class in Python With Examples
No comments:
Post a Comment