Menu
Topics Index
...
`


Operators >
Siva Nookala - 12 Apr 2016
The relational operators determine the relationship between two operands. e.g., 3 is greater than 2, 25 is less than 30.

The following six relational operators are supported by Java.
Operator Result
== Equal to
!= Not Equal to
> Greater than
< Less than
>= Greater than or equal
<= Less than or equal
  • Equal to (==) and Not equal to (!=) can be applied on any type, including integers, floating-point numbers, characters and booleans.
  • Ordering operators - less than, greater than, less than or equal and greater than or equal - can be used only on numeric types - integers, floating point numbers and characters. Booleans are not included.
  • Single equal to (=) is assignment operator, which is diff from (==) double equal to which compares two operands.
  • The result produced by relational operator is always a boolean value. i.e. the value is either true or false
Print number relations
class PrintNumberRelations
{
    public static void main(String arg[])
    {
        int a = 4;
        int b = 1;
        boolean x = ( a > b );
        boolean y = ( a <= b );
        boolean z = ( a == b );
        
        System.out.println(" The statement - a greater than b - is " + x);
        System.out.println(" The statement - a is less than or equal to b - is " + y);
        System.out.println(" The statement - a is equal to b - is " + z);    
    }
}
OUTPUT

The statement - a greater than b - is true
The statement - a is less than or equal to b - is false
The statement - a is equal to b - is false

DESCRIPTION

Here the relation between a and b is found using various relational operators. x tells if a is greater than b (a > b), y tells if a is less than or equal to b (a <= b) and z tells if a and b are equal (a == b).

THINGS TO TRY
  • Add a new boolean w which tells if a greater than or equal to b and print the result.
  • Add a new boolean v which tells if a is not equal to b and print the result.
  • Add a new boolean u which tells if ((a + 2) * 3) is less than ((b + 1) * 9) and print the result.

0
Wrong
Score more than 2 points

© meritcampus 2016 - 2017

All Rights Reserved.