Equality, Relational, & Conditional Operators

Equality & Relational Operators

The Equality Operator and Relational Operator determine if the first operand is a) greater than, b) less than, c) equal to, or d) not equal to the other operand. As a Java programmer, most of these operators you will have seen or used already. Don’t forget that “==” and “=” are not the same, the latter is used when assigning values.

Operator Description
== equal to
!= not equal to
> greater than
>= greater than or equal to
< less than
<= less than or equal to


Equality & Relational Operators Samples

int jcdInteger1 = 100;
int jcdInteger2 = 200;

//Equal to        
if(jcdInteger1 == jcdInteger2)
    System.out.println("jcdInteger1 == jcdInteger2");

//Not equal to        
if(jcdInteger1 != jcdInteger2)
    System.out.println("jcdInteger1 != jcdInteger2");
  
//Greater than      
if(jcdInteger1 > jcdInteger2)
    System.out.println("jcdInteger1 > jcdInteger2");

//Greater than or equal to        
if(jcdInteger1 >= jcdInteger2)
    System.out.println("jcdInteger1 >= jcdInteger2");

//Less than
if(jcdInteger1 < jcdInteger2)
    System.out.println("jcdInteger1 < jcdInteger2");
    
//Less than or equal to 
if(jcdInteger1 <= jcdInteger2)
    System.out.println("jcdInteger1 <= jcdInteger2");


Equality & Relational Operators Result

jcdInteger1 != jcdInteger2
jcdInteger1 < jcdInteger2
jcdInteger1 <= jcdInteger2


Conditional Operator

The Conditional AND ( && ) and Conditional OR ( || ) operators perform operations on two boolean expressions. Both of these operators exhibit a “short-circuiting” behavior. This means that the second operand is evaluated only if needed.

Operator Description
&& Conditional AND
!! Conditional OR


Conditional Operators Sample

int jcdInteger1 = 100;
int jcdInteger2 = 200;
    
//Conditional AND    
if((jcdInteger1 == 100) && (jcdInteger2 == 200))
System.out.println("jcdInteger1 is 100 AND jcdInteger2 is 200");
        
//Conditional OR
if((jcdInteger1 == 100) || (jcdInteger2 == 100))
System.out.println("jcdInteger1 is 100 OR jcdInteger2 is 100");


Conditional Operators Output

jcdInteger1 is 100 AND jcdInteger2 is 200
jcdInteger1 is 100 OR jcdInteger2 is 100


Now that your done with lesson 6.2, be sure to move onto the next lesson.



Add a Comment

Your email address will not be published. Required fields are marked *