Java Program to Illustrate Use of Various Boolean Operators

This is a Java Program to Illustrate the Use of Various Boolean Operators.

Enter any two boolean values as input. After that we perform various boolean bitwise opeartions like AND, OR, XOR and NOT to get desired output.

Here is the source code of the Java Program to Illustrate the Use of Various Boolean Operators. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

  1. import java.util.Scanner;
  2. public class Boolean_Operators 
  3. {
  4.     public static void main(String args[]) 
  5.     {
  6.         Scanner s = new Scanner(System.in);
  7.         System.out.print("Enter a:");
  8.         boolean a = s.nextBoolean();
  9.         System.out.print("Enter b:");
  10.         boolean b = s.nextBoolean();
  11.         boolean c = a | b;
  12.         boolean d = a & b;
  13.         boolean e = a ^ b;
  14.         boolean f = (!a & b) | (a & !b);
  15.         boolean g = !a;
  16.         System.out.println("a = " + a);
  17.         System.out.println("b = " + b);
  18.         System.out.println("a|b = " + c);
  19.         System.out.println("a&b = " + d);
  20.         System.out.println("a^b = " + e);
  21.         System.out.println("!a&b|a&!b = " + f);
  22.         System.out.println("!a = " + g);
  23.     }
  24. }

Output:

$ javac Boolean_Operators.java
$ java Boolean_Operators
 
Enter a:true
Enter b:false
a = true
b = false
a|b = true
a&b = false
a^b = true
!a&b|a&!b = true
!a = false

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

advertisement
advertisement

Here’s the list of Best Books in Java Programming, Data Structures and Algorithms.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.