Java Comparison Operators
There are six comparison or relational operations in Java:
The equality operator (==) is used to compare if two values are the same. This returns true if both vales are equal.
We can modify this with the NOT operator (!) to change equality (==) to inequality (!=). This returns true if both values are not equal.
The greater than operator (>) is used to check if one value is larger than another and will return true if it is.
The less than operator (<) is used to check if one value is smaller than another and will return true if it is.
We can combine the greater than operator with the comparison operator (>=) to check if one value is greater than or equal to another value.
We can combine the less than operator with the comparison operator (<=) to check if one value is less than or equal to another value.
Operator | Comparative Test | Example | Outcome |
---|---|---|---|
== | Equality | 10==5 | false |
!= | Inequality / Not Equal To | 10!=5 | true |
> | Greater Than | 10>5 | true |
< | Less Than | 10<5 | false |
>= | Greater Than or Equal To | 10>=5 | true |
<= | Less Than or Equal To | 10<=5 | false |