×

Types of Logical Operators in Java

In this tutorial, we are going to study logical operators. A logical operator is an operator that accomplishes a logical operation that is to connect two or more operations. These operators are of great importance in java as it helps to perform several crucial operations in java.

We will understand the types of logical operators with the aid of certain examples as well as some programs.

With the help of logical operators, one can execute logical “AND”, “OR” and “NOT” operations, just like “AND”, “OR” and “NOT” operations in digital electronics.

The gist of three logical operators.

  1. AND Operator && ) – if( a && b ) [if both the conditions are true, it executes otherwise it doesn’t]
  2. OR Operator ( || ) – if( a || b) [if one of the conditions is true, it executes otherwise it doesn’t]
  3. NOT Operator ( ! ) – !(a<b) [It returns false if the value of a is less than that of  b]

Now, let us try to understand each of the three operators in detail through examples and java code.

AND Operator

Syntax - if(a && b )

Here, a refers to condition 1 and b refers to condition 2.

Itreturns only if both the conditions are true, otherwise, it doesn’t.

Its symbol is &&.

Example

x = 12, y = 15, z = 22

To check if the variable z is the largest among the three.

If both of these conditions are satisfied, only then we can conclude that z is the largest.

if(z > x && z > y)

Condition 1: if (z > x) // (22 > 12) // TRUE

Condition 2: if (z > y) // (22 > 15) // TRUE

Therefore, z is the largest.

If both of these conditions are satisfied, only then we can conclude that z is the largest.

Truth Table:

aba && b
111
100
010
000
  • If condition 1 and condition 2 are true, the resultant is also true.
  • If condition 1 is true and condition 2 is false, the resultant is false.
  • If condition 1 is false and condition 2 is true, the resultant is false.
  • If condition 1 is false and condition 2 is false, the resultant is false.

Implementation

Now, we will see a java program to understand the concept even better.

Example 1:

// Java code to illustrate the working of
// logical AND operator


import java.io.*;


class Logical_operator {
    public static void main(String[] args)
    {
        // initializing the variables
        int a1 = 35, a2 = 45, a3 = 12, a4 = 0;


        // Displaying a, b, c
        System.out.println("Variable 1 = " + a1);
        System.out.println("Variable 2 = " + a2);
        System.out.println("Variable 3 = " + a3);


        // using the logical AND to verify


        if ((a1 < a2) && (a2 == a3)) {
            a4 = a1 + a2 + a3;
System.out.println(" sum = " + a4);
        }
        else
System.out.println(" conditions are False ");
    }
}

Output:

Types of Logical Operators in Java

Explanation: In this java code, an illustration of the logical AND operator is shown.

Example 2:

import java.io.*;


class shortCircuit_effect {
public static void main(String[] args)
{


// initializing the variables
int a1 = 30, a2 = 40, a3 = 36;


// printing the value of variable b
System.out.println("Value of a2 : " + a2);


// Using logical AND operator
// Short-Circuiting effect as condition 1 is
// incorrect, thus condition 2 is never been touched
// and so ++a2 (pre-increment) doesn't take place and
// the value of a2 remains unchanged
if ((a1 > a3) && (++a2 > a3)) {
System.out.println("Inside the if block");
}


// Printing the value of a2
System.out.println("Value of a2 : " + a2);
}
}

Output:

Types of Logical Operators in Java

Explanation: In this java code, an illustration of the logical AND operator is shown. Here, the case of the short circuit effect is illustrated.

OR Operator 

Syntax – if(a || b)

Here, a refers to condition 1 and b refers to condition 2.

Itreturns even if the only condition is true. If the first operand is true, it doesn’t even check the second operand.

Its symbol is ||.

Example

x = 5, y = 8

z = (x > 0 || y > 0) // (5 > 0 || 8 > 0)

5 > 0 // TRUE

The first condition is true, so the resultant is true and there is no need to check the second condition.

Truth Table:

aba && b
111
101
011
000

If condition 1 and condition 2 are true, the resultant is also true.

If condition 1 is true and condition 2 is false, the resultant is true as the working of the OR operator is such that even if one of the conditions is true, the output is also true.

If condition 1 is false and condition 2 is true, the resultant is true.

If condition 1 is false and condition 2 is false, the resultant is false.

Implementation:

Now, we will see a java program to understand the concept even better.

// Java code to understand the working
// logical OR operator


import java.io.*;


class Logical_operator {
public static void main(String[] args)
{
// initializing variables
    int a1 = 35, a2 = 45, a3 = 12, a4 = 0;


// printng the values of a1, a2, a3
System.out.println("Variable 1 = " + a1);
System.out.println("Variable 2 = " + a2);
System.out.println("Variable 3 = " + a3);
System.out.println("Variable 4 = " + a4);


// logical OR operators being used

if (a1 > a2 || a3 == a4)
System.out.println("Either of the two conditions are true");
else
System.out.println("Neither of the two conditions are true");
}
}

Output:

Types of Logical Operators in Java

Explanation: In this java code, an illustration of the logical OR operator is shown.

NOT Operator 

Syntax -!(condition)

If the value is true, it will return false, and vice versa.

Its symbol is !.

Example

y = !(0)

output – 1

y = !(x == 5)

output - 0

a = 5, b = 7

!(a<b) // !(5 < 7) // !(true) // false

It complements the result. It gives out false if the result was true and true if the result was false.

Truth Table:

ab
01
10

Implementation:

Now, we will see a java program to understand the concept even better.

// Java code to understand the working of
// logical NOT  in java


import java.io.*;


class Logical {
public static void main(String[] args)
{
// initializing the variables
int a1 = 22, a2 = 1;


// Printing the values of a1 and a2
System.out.println("Variable 1 = " + a1);
System.out.println("Variable 2 = " + a2);


//  logical NOT operator being used
System.out.println("!(a1 < a2) = " + !(a1 < a2));
System.out.println("!(a1 > a2) = " + !(a1 > a2));
}
}

Output:

Types of Logical Operators in Java

Explanation: In this java code, an illustration of the logical NOT operator is shown.

Example of Logical Operator

Illustrating the working of logical AND, logical OR, and logical NOT operators together in one java program.

class Logical_operators {
  public static void main(String[] args) {


    int a = 4, b = 6;
    // illustrate the working of && operator
System.out.println("Outputs of AND operator"); 
boolean x = ((a > b) && (a > b));
System.out.println(x);  
    x = ((a > b) && (a < b));
System.out.println(x);  


    // illustrate the working of || operator
System.out.println("Outputs of OR operator"); 
    x = (a < b) || (a > b);
System.out.println(x);
    x = (a > b) || (a < b);
System.out.println(x);  
    x = (a < b) || (a < b);
System.out.println(x);  
    x = (a > b) || (a > b);
System.out.println(x);


    // illustrate the working of! operator
System.out.println("Outputs of NOT operator"); 
    x = !(a == b);
System.out.println(x);  
    x = !(a > b);
System.out.println(x);  
  }
}

Output:

Types of Logical Operators in Java

Explanation: In this java code, an illustration of the logical AND, logical OR, and logical NOToperatorsare shown.

Summary

This tutorial was all about the types of logical operators in Java language. We saw the three logical operators namely – logical AND, logical OR, and logical NOT. We saw the syntax, truth table, and working of each of these operators and finally, undertook a practical approach for which we wrote java codes to understand the concept even better.


Related Topics

Java copy file

There are for the most part 3 methods for duplicating documents utilizing java language. They are as given underneath: Utilizing File StreamUtilizing FileChannel ClassUtilizing Files class. 1. Using File Stream: Here we are...

5 minutes read.

Java this keyword

This Keyword in Java This keyword can be used in many different ways in Java. This is a reference variable in Java that points to the active object. In Java, the...

8 minutes read.

Java protected vs private

Java : Java is a pure object oriented language. It was introduced by James Gosling in the year 1995. The first public implementation of java was done by sun micro systems...

3 minutes read.

CopyOnWriteArrayList in Java

The CopyOnWriteArrayList is used to implement the List Interface. It is the improved version of ArrayList. The operations like add, remove, set, update etc, these operations are done by creating...

5 minutes read.

Fibonacci Series Program in Java

Fibonacci Series Program in Java using Recursion Fibonacci series is a series whose every term is comprised of adding its previous two terms, barring the first two terms 0 and 1....

3 minutes read.

Java Integer toUnsignedLong() method

The toUnsignedLong() method of Java Integer class returns a long value by simply converting the given argument to long after an unsigned conversion. Syntax public static long toUnsignedLong (int  x) Parameters The parameter ‘x’...

1 minute read.

Ramanujan Number or Taxicab Number in Java

In this section, we will discuss what a Ramanujan number (also known as a Hardy-Ramanujan number) is and how to use a Java programme to determine if a given integer...

3 minutes read.

Buffer reader to read string in Java

The Buffered Reader class of Java is used to read the stream of characters from the input stream. Program to read string using Buffer reader import java.io.*; class  Demo {   public static void main(String...

3 minutes read.

Number Pattern Programs in Java

Number Pattern Programs in Java: Number pattern programs are part of pattern programs. In the previous section, we have learned the approach to print the pattern program in Java. To...

6 minutes read.

Monsoon Umbrella Problem in Java

The Monsoon Umbrella problem is a classic Java programming problem used to test the skills of a programmer. The problem involves writing a program to determine the number of umbrellas...

3 minutes read.

Find Unique Elements in Array Java

An array in Java is a grouping of objects that share the same type of data. We are free to enter identical or repeating elements into an array. Therefore, we...

6 minutes read.

Generic queue in Java

Before understanding how to implement a generic queue in java, one must know about generics and queue in java. Generics in Java Generics are parameterized types. The goal is to enable type...

6 minutes read.

Java Thread class

Thread class The thread represents a part of the process. Every process can have multiple associated threads in which every thread may execute the same or different job. By default, each thread assigns...

16 minutes read.

RMI program in Java

Remote Method Invocation is what it stands for. An object can call the method of another object in a different space address using the RMI API, which may be on the...

3 minutes read.

Java Rename File

Renaming a file is the process of changing its name. Using the renameTo() function of the Java File class, renaming operations are possible. A file can be renamed using Java's renameTo()...

3 minutes read.

Applet Life Cycle in Java

In this article, we are going to acknowledge you about what a applet is, what is its life cycle and stages in life cycle, along with the syntax and example...

4 minutes read.

How to check Date Null in Java?

In this section, we will be acknowledged about Date Null in Java. The date null in Java is an entity that is used when there is no specified value for...

3 minutes read.

Undo and Redo Operations in Java

Undo and redo operation are the most widely used operation while dealing with file. In this section, we will discuss how to implement undo and redo operation in Java. Undo Redo...

2 minutes read.

Java Developer

Who is a Java Developer? A Java developer is a skilled programmer who works on commercial applications, software, and webpages.  Java developers can work in two different areas: Operating system development:...

3 minutes read.

How to Calculate Week Number From Current Date in Java?

The WeekFields class's weekOfMonth() method is utilized to return the field for access the week of a month based on this WeekFields. If the first day of the month is a...

3 minutes read.