Logical Operators in Java Example Program

Boolean operators in java. A java program that demonstrates how to use various Boolean operators. As input, choose any two boolean values. Then, to get the desired output, we use various boolean bitwise operations such as AND, OR, XOR, and NOT. 

import java.util.Scanner;
public class Main
{
public static void main(String args[])
{
Scanner s = new Scanner(System.in);
System.out.print("Enter a:");
boolean a = s.nextBoolean();
System.out.print("Enter b:");
boolean b = s.nextBoolean();
boolean c = a | b;
boolean d = a & b;
boolean e = a ^ b;
boolean f = (!a & b) | (a & !b);
boolean g = !a;
System.out.println("a = " + a);
System.out.println("b = " + b);
System.out.println("a|b = " + c);
System.out.println("a&b = " + d);
System.out.println("a^b = " + e);
System.out.println("!a&b|a&!b = " + f);
System.out.println("!a = " + g);
}
}

 

Output:

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

Boolean Operator in Java

The primitive data type boolean accepts either “true” or “false” values. As a result, anything that returns the value “true” or “false” is a boolean example. Some circumstances, such as “a==b,” “ab,” or “a>b,” can be thought of as boolean examples.

Boolean Operators (AND, OR, NOT, or AND NOT) are basic words that are used as conjunctions to combine or exclude keywords in a search, resulting in more focused and productive results. This should save time and effort by removing irrelevant hits that would otherwise have to be scanned before being discarded.

Operators in Java Program

Operators are a subset of functions that take one or more arguments and return a new value. Operators include things like addition (+), subtraction (-), multiplication (*), and so on. Variables and constants are operated on with the use of operators. Using this program we can execute boolean operators in java.