Friday 21 October 2016

Relational Operators

Relational operators

  • Relational Operators are,
      • ==    Equal to
      • !=     Not equal to
      • >      Greater than
      • >=    Greater than or equal to
      • <      Less than
      • <=    Less than or equal to
❏ The relational operators are used to compare two operands or two expressions and produces the result as a boolean value.

Operator
Meaning
==
equal to
!=
not equal to
greater than
>=
greater than or equal to
less than
<=
less than or equal to
  ---------------------------------------------------------------------------------------
Program         :       Program by using relational operators
Program name  :       ROpeDemo1.java
Output                  
                              x == y? false
      x != y? true
      x > y? false
      x >= y? false
      x < y? true
      x <= y? True

class ROpeDemo1
{
        public static void main(String args[])
        {
               int x = 10;
               int y = 20;
               boolean equalsResult = (x == y);
               int notEqualsResult= x != y;
               int greaterThanResult= x > y;
               int greaterThanOrEqualsResult= (x >= y);
       int lessThanResult= x < y;
       int lessThanOrEqualsResult= (x <= y);

               System.out.println("x == y? " + equalsResult);
               System.out.println("x != y? " + notEqualsResult);
               System.out.println("x > y? " + greaterThanResult);       
               System.out.println("x >= y? " + greaterThanOrEqualsResult);
               System.out.println("x < y? " + lessThanResult);
               System.out.println("x <= y? " + result);
        }
}

Compile     :            javac ROpeDemo1.java
Run           :            java ROpeDemo1
Output             
                              +x = 10
      -y = -20
      ++x = 11
      ++y = 19
      false
---------------------------------------------------------------------------------------
Thanks for your time,
Nireekshan