What are Equality, Relational, and Conditional Operators in java


Equality, Relational, and Conditional Operators >

Equality Operators

==
equal to
!=     
not equal to


Relational Operators

>      
greater than
>=     
greater than or equal to


<      
less than
<=    
less than or equal to


Conditional Operators

&&
Conditional AND

operator1
operator2
Result
true
true
true
true
false
false
false
true
false
false
false
false
||
Conditional OR

operator1
operator2
Result
true
true
true
true
false
true
false
true
true
false
false
false

Ternary operator

?:
ternary operator


Program 3.1 to demonstrate equality operators
/** JavaMadeSoEasy.com */
public class EqualityOperatorTest {
   public static void main(String[] args) {
          int x=4;
          int y=2;
         
          System.out.println(x==y); //false    
          System.out.println(x!=y); //true
   }
}
/*OUTPUT
false
true
*/


Program 3.2 to demonstrate relational operators
/** JavaMadeSoEasy.com */
public class RelationalOperatorTest {
   public static void main(String[] args) {
          int x=4;
          int y=2;
          System.out.println(x>y); //true      
          System.out.println(x>=y); //true     
         
          System.out.println(x<y); //false
          System.out.println(x<=y); //false
         
          System.out.println(x>=4); //true
          System.out.println(y<=2); //true
         
   }
}
/*OUTPUT
true
true
false
false
true
true
*/


Program 3.3 to demonstrate conditional operators
/** JavaMadeSoEasy.com */
public class ConditionalOperatorTest {
   public static void main(String[] args) {
          int x=4;
          int y=2;
          System.out.println("---------  && -----------");
         
          System.out.println((x==4) && (y==2)); //true -->> (x==4) returns true, (y==2) also returns true,
                                                                     //So,  (true && true) returns true
          System.out.println((x==7) && (y==2)); //false -->> (x==7) returns false, so (y==2) is not evaluated.
                                                                     //So,  (false && (true/false)) returns false
          System.out.println((x==4) && (y==9));  //false -->> (x==4) returns true, but (y==2) returns false.
                                                                     //So,  (true && false) returns false
          System.out.println("---------  || -----------");
         
          System.out.println((x==4) || (y==8));  //true -->> (x==4) returns true, so (y==8) is not evaluated.
                                                                            //So,  (true || (true/false)) returns true
          System.out.println((x==7) || (y==2));   //true -->> (x==7) returns false, (y==2) returns true
                                                                            //So,  (false || (true/false)) returns true
          System.out.println((x==9) || (y==8)); //false  -->> (x==9) returns false, (y==2) also returns false
                                                                            //So,  (false || false) returns false
   }
}
/*OUTPUT
---------  && -----------
true
false
false
---------  || -----------
true
true
false
*/


Program 3.4 to demonstrate ternary operator >
/** JavaMadeSoEasy.com */
public class TernaryConditionalOperatorTest {
   public static void main(String[] args) {
          int x=3;
          System.out.println( (x >4) ?   "x is > 4"   :   "x is < 4" );
          System.out.println( (x==3) ?   "x == 3" :   "x != 3" );
         
   }
}
/*OUTPUT
x is < 4
x == 3
*/

No comments:

Post a Comment