Logical Operators In Java With Example Program

Various type of operators in java



There are three types of Logical Operators In Java

No Operator USD Description
1     &&  A && B Logical AND
2     || A||B Logical OR
3     ! A!=B Not

1) Logical AND


  • This logical operator called Logical AND operator
  • Logical And operator in both the condition is true that time give the true answer
  • if any condition is false that time gives the false answer it gives in the example

Example
public class LogicalAnd {
 
 public static void main(String[] args) {
  
 int m1 = 10;
 int m2 = 20;
 int m3 = 15;
                   //true && true
 System.out.println(m1<m2 && m2>m3);
                   //true && false
 System.out.println(m1<m2 && m2<m3);
                   //false && true
 System.out.println(m1>m2 && m2>m3);
                   //false && false
 System.out.println(m1>m2 && m2<m3);
    
    }
}

Output
true
false
false
false


2) Logical OR


  • This logical operator call Logical OR operator
  • Logical OR operator in the check both condition and in both condition is true then give the true 
  • The first condition is true and the second condition is false then give the false answer
  • The first condition is false and the second condition is true then gives the true answer
  • If both the condition is false then gives the false answer 
Example
public class LogicalOR {
 public static void main(String[] args) {
  int s1 = 10;
  int s2= 34;
  int s3 = 45;
  
                    //true || true
  System.out.println(s1<s2 || s2<s3);
                    //true || false
  System.out.println(s1<s1 || s2>s3);
                   //false || true
  System.out.println(s1>s2 || s2<s3);
                   //false || false
  System.out.println(s1>s2 || s2>s3);
 
 }
}

Output
true
false
true
false

3)  Logical NOT



  • This logical operator called as Logical NOT operator
  • use to reverse the logical state of its operand if the condition is true then logical not operator gives the false answer in the use of logical And operator
  • in case of the first condition is false and the second condition is true then answer is true but the use of logical And operator answer is the reverse is false
Example
public class NotOperator {
 public static void main(String[] args) {
  boolean s1 = true;
  boolean s2= false;
  
  System.out.println("not equals and value "+ !(s1 && s2) );
  System.out.println("not equals or value "+ !(s2 || s1) );
  
 
 }
}

Output
not equals and value true
not equals or value false

so it's all about in Logical Operators In Java With Example Program

Post a Comment

Previous Post Next Post