Java Tutorial

Java Control Statements

Object Oriented Programming

Java Built-in Classes

Java File Handling

Java Error & Exceptions

Java Multithreading

Java Synchronization

Java Networking

Java Collections

Java List Interface

Java Queue Interface

Java Map Interface

Java Set Interface

Java Data Structures

Java Collections Algorithms

Java Miscellaneous

Advanced Java

Java APIs & Frameworks

Java Useful Resources

Java - Logical Operators



The following programs are simple examples which demonstrate the logical operators. Copy and paste the following Java programs as Test.java file, and compile and run the programs −

Example 1

In this example, we're creating two variables a and b and using logical operators. We've performed a logical AND operation and printed the result.

public class Test {

   public static void main(String args[]) {
      boolean a = true;
      boolean b = false;

      System.out.println("a && b = " + (a&&b));
   }
}

Output

a && b = false

Example 2

In this example, we're creating two variables a and b and using logical operators. We've performed a logical OR operation and printed the result.

public class Test {

   public static void main(String args[]) {
      boolean a = true;
      boolean b = false;

      System.out.println("a || b = " + (a||b) );
   }
}

Output

a || b = true

Example 3

In this example, we're creating two variables a and b and using logical operators. We've performed a logical Negate operation and printed the result.

public class Test {

   public static void main(String args[]) {
      boolean a = true;
      boolean b = false;
	  
      System.out.println("!(a && b) = " + !(a && b));
   }
}

Output

!(a && b) = true
java_basic_operators.htm
Advertisements