Java Operators - Relational Operators

March 7, 2018 Oclemy Java Operators 0 minutes, 46 seconds

Relational Operators are also known as comparison operators.

They are used to compare two values and return a boolean result.

Here are the list of relational operators;

Java Operator Mathematical Symbol Name Example (diameter = 10) Result
< < less than diameter < 0 false
<= less than or equal to diameter <= 0 false
> > greater than diameter > 0 true
>= greater than or equal to diameter >= 0 true
== = equal to diameter == 0 false
!= not equal to diameter != 0 true

NOTE: To test equality you use two equal signs(++), not a singke equal sign(=). The latter is used for assignment.

The result from a comparison is either true or false.

Example:

package info.tutorialsloop;

public class Main {

    public static void main(String[] args) {

        double diameter=19.5;
        System.out.println(diameter > 0);
        System.out.println(diameter < 0);
        System.out.println(diameter >= 0);
        System.out.println(diameter <= 0);
        System.out.println(diameter == 0);
        System.out.println(diameter != 0);
    }
}

RESULT

true
false
true
false
false
true

Comments