Relational Operators



Relational operators specifies relationship between two operands. This relationship basically include equality and ordering type of relation. Relational operator in java return boolean data type on the evaluation of relational operator.

In java evaluation of relational expression return boolean



Options Usage
== equality operator
!= Not equal operator
< less than operator
> greater than
<= less than equal to
>= greater than equal to


Equality Operator

Equality operator is used to check whether two values are equal or not. This operator also can be used to check if two reference variable are pointing to same object or not. Equality operator and not equal operator can be applied on integer, floating, boolean and char data type. Equality operator is denoted by double equal sign "==". Remember this single equal sign is used as assignment operator.



//file:relate.java
//www.jgyan.com
class relate
 {
   public static void main(String arg[])
    {
	  int a=10;
	  int b=10;
	  boolean c = a < b;
	  System.out.println(c);
	  if(a==b)
	    System.out.println("a and b are equal");
      else
System.out.println("a and b are not equal" ); } } /*output false a and b are equal */


	// Now allowed in Java
	 int flag=11;
	  if(flag) 
	//Condition in Java
	  boolean flag=true;
	  if(flag).....  // In java only boolean data type can be used
	 



//file:relate1.java
//www.jgyan.com
class relate1
 {
   public static void main(String arg[])
    {
	  int a=10;
	  int b=16;
	  if(a=b) // Single equal sign is used as assignment operator. it will  be always true.
	    System.out.println("a and b are equal");
      else
	    System.out.println("a and b are not equal" );
	  }
 }
 /*output
a and b are equal
*/
	


Above program demonstrating mistake frequently done by programmer, Instead of equality operator if assignment operator used then program will exhibit unexpected behaviour.

Example of '<', '>' , '<=' and '>='

//file:relate2.java
//www.jgyan.com
class relate2
 {
   public static void main(String arg[])
    {
	  int a=10;
	  int b=16;
	  if(a<b) // Single equal sign is used as assignment operator. it will  be always true.
	    System.out.println("a is less than b");
      else
	    System.out.println("b is less than a" );
		
		if(a<=b) 
	    System.out.println("a is less than equal to b");
      else
	    System.out.println("b is less than equal to a" );
	  }
 }
 /*output
a is less than b
a is less than equal to b
*/