BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Logical Operators in Java with Examples

Last Updated: October 15, 2022 by Chaitanya Singh | Filed Under: java

Logical Operators are used to evaluate the outcome of conditions. There are three logical operators in java: AND (&&), OR (||) and NOT (!). The AND and OR operators are used when multiple conditions are combined and we need to evaluate the outcome as a whole.

AND Operator: It returns true if all the conditions are true.

OR Operator: It returns true if any of the condition is true.

NOT Operator: It returns true if condition is false, returns false if condition is true.

This can be represented by a truth table like this:

Logical Operators Truth Table in Java

Logical ‘AND’ Operator (&&) Example

Let’s say, an institute grants admission based on the student marks. If student math marks represented by variable mathVar and science marks represented by scienceVar both are greater than 95 then the admission is granted, else the admission is not granted.

This condition can be written using AND operator and if-else statement like this:

class JavaExample {
  public static void main(String[] args) {
    int mathVar = 94, scienceVar =99;

    //checking whether marks in both the subjects are
    //greater than 95
    if (mathVar >=95 && scienceVar>=95){
      System.out.println("Admission granted.");
    }
    else{
      System.out.println("Sorry! Admission is not granted.");
    }
  }
}

Output: Admission is not granted because both the conditions are not true.

Output of example

Logical ‘OR’ Operator (||) Example

Case 2: Let’s say, another institute grants admission if marks in any one of the subject is greater than 98. This means, if student scored more than 98 in any one of the subject then the admission is granted in this institute. In this case, we can use OR Operator as we need any one of the condition to return true.

class JavaExample {
  public static void main(String[] args) {
    //let's consider the marks of the same student that we have seen
    //in the first example
    int mathVar = 94, scienceVar =99;

    //checking whether marks in any one of the subject is greater
    //than 98
    if (mathVar >=98 || scienceVar>=98){
      System.out.println("Admission granted.");
    }
    else{
      System.out.println("Sorry! Admission is not granted.");
    }
  }
}

Output:

output OR operator example

Logical ‘NOT’ Operator (!) Example

Case 3: There is a third institute that grants admission, if marks in math subject is not less than 75. Let’s write this case using NOT Operator.

class JavaExample {
  public static void main(String[] args) {
    int mathVar = 94, scienceVar =99;

    //In this case, marks in science subject is not required
    //to be checked.
    if (!(mathVar<75)){
      System.out.println("Admission granted.");
    }
    else{
      System.out.println("Sorry! Admission is not granted.");
    }
  }
}

Output:

Admission granted.

Short-circuit in Logical Operators

Short circuit means that the compiler doesn’t evaluate the right side condition if it is not necessary. This happens while evaluating the conditions using && and || operators.

In every AND and OR operators operation, both sides are evaluated except the following case:

If first condition returns false in AND operation:
false && …… : it is not necessary to evaluate the right-hand side because the result can only be false regardless of the condition on right side. This is because AND returns false, if any one of the condition is false.

If first condition returns true in OR operation:
true || ……. : it is not necessary to evaluate the right-hand side because the result can only be true regardless of the conditions on the right side. This is because OR returns true, if any one of the condition is true.

Short Circuit Example

In this example, you would think that a compiler checks for both the conditions 1==2 and 5+5==10. However this is not happening, when compiler checks for first condition and finds the result false, it knows that the output for whole expression cannot be true whatever the outcome of second condition so it doesn’t check the second condition.

class JavaExample {
  public static void main(String[] args) {
    if(1==2 && 5+5 ==10){
      System.out.println("Hello");
    }else{
      System.out.println("Bye");
    }
  }
}

Recommended Posts

  • Arithmetic Operators in Java with Examples
  • Assignment Operators in Java with Examples
  • Unary Operators in Java with Examples
  • Relational Operators in Java with Examples
  • Bitwise Operators in Java with Examples
  • Ternary Operator in Java with Examples
  • Shift Operators in Java with Examples
❮ Operators in Java

Top Related Articles:

  1. Java Variables: Declaration, Scope, and Naming Conventions
  2. Unary Operators in Java with Examples
  3. Can Static Methods be Overloaded or Overridden in Java?
  4. Relational Operators in Java with Examples
  5. Arithmetic Operators in Java with Examples

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Java Tutorial

Java Introduction

  • Java Index
  • Java Introduction
  • History of Java
  • Features of Java
  • C++ vs Java
  • JDK vs JRE vs JVM
  • JVM - Java Virtual Machine
  • First Java Program
  • Variables
  • Data Types
  • Operators

Java Flow Control

  • Java If-else
  • Java Switch-Case
  • Java For loop
  • Java while loop
  • Java do-while loop
  • Continue statement
  • break statement

Java Arrays

  • Java Arrays

OOPs Concepts

  • OOPs Concepts
  • Constructor
  • Java String
  • Static keyword
  • Inheritance
  • Types of inheritance
  • Aggregation
  • Association
  • Super Keyword
  • Method overloading
  • Method overriding
  • Overloading vs Overriding
  • Polymorphism
  • Types of polymorphism
  • Static and dynamic binding
  • Abstract class and methods
  • Interface
  • Abstract class vs interface
  • Encapsulation
  • Packages
  • Access modifiers
  • Garbage Collection
  • Inner classes
  • Static import
  • Static constructor

Java Exception Handling

  • Exception handling
  • Java try-catch
  • Java throw
  • Java throws
  • Checked and Unchecked Exceptions
  • Jav try catch finally
  • Exception Examples
  • Exception Propagation

Collections Framework

  • Collections in Java
  • Java ArrayList
  • Java LinkedList
  • Java Vector
  • Java HashSet
  • Java LinkedHashSet
  • Java TreeSet
  • Java HashMap
  • Java TreeMap
  • Java LinkedHashMap
  • Java Queue
  • Java PriorityQueue
  • Java Deque
  • Comparable interface
  • Comparator interface
  • Collections Interview Questions

MORE ...

  • Java Scanner Class
  • Java 8 Features
  • Java 9 Features
  • Java Conversion
  • Java Date
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations
  • Java main method
  • Java Interview Q

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap