Logical Operators Example in Java

Write a Program to illustrate Logical Operators in Java. The program should clearly demonstrate the working and supported data types of Logical Operator in Java.

These operators conduct logical "AND," "OR," and "NOT" operations, which are comparable to the AND and OR gates in digital electronics. They're used to integrate two or more conditions/constraints or to supplement the evaluation of a specific condition. One point to bear in mind is that if the first condition is false, the second condition is not examined, resulting in a short-circuiting effect. Used to test for a variety of conditions before deciding.

Try your Solution

Strongly recommended to Solve it on your own, Don't directly go to the solution given below.

public class Hello { public static void main(String args[]) { //Write your code here } }

Program or Solution


import java.util.Scanner;

class L_Operator


public static void main(String args[])


int num_1 = 10;

int num_2 = 20;

char ch_1 = 'D';

char ch_2 = 'E';

String name_1 = "Decode";

String name_2 = "Decode";

boolean val_1 = true;

boolean val_2 = false;

//Logical Operation on  Numbers like int and float are not acceptable

        //System.out.println(num_1 && num_2);

//System.out.println(num_1 || num_2);

//Logical Operation on Characters are not acceptable

//System.out.println(ch_1 && ch_2);

//System.out.println(ch_1 || ch_2);

//Logical Operation on Strings are not acceptable

//System.out.println(name_1 && name_2); 

//System.out.println(name_1 || name_2); 

//Logical Operation on boolean are allowed

System.out.println(val_1 && val_2);

System.out.println(val_1 || val_2);

System.out.println(!val_2); // unary Operator

//Logical operation on Relational Experession

System.out.println(num_1 < num_2 && num_1 != 0);

System.out.println(num_1 < num_2 || num_1 != 0);

System.out.println(!(num_1 < num_2)); // unary Operator




Logical Operators Example in Java Output

Program Explanation

OperatorBooleanExpression Which Returns BooleanAny Other Data Type
Logical AND (&&)    ✓
Logical OR (||)     ✓                                ✓
Logical Not (!)     ✓                                ✓

The Following Table Demonstrates the Data Type supported for Logical Operators.