Search
  • +44-7459919437 (UK- WhatsApp & Direct Call) | +91-6383544892 (India - WhatsApp Only) | Email Id : vinothrwins@gmail.com
Vinoth Tech Solutions
  • Home
  • Selenium Java Online Training
  • Self Paced Video Course
    • Selenium Course Curriculum
    • Cypress Course Curriculum
    • Playwright Course Curriculum
  • Tutorials
  • Demo Sites
    • E-Commerce Demo Application
    • Practice Automation
      • Demo Page Healthcare
      • Registration Form
      • Transaction Details
      • DropDown
      • Mouse Event
      • Keyboard Events
      • Alert and Popup
      • Multiple Windows
      • iFrames
      • Wait WebElement
      • WebTable
  • FAQS
  • About Me & Feedback
    • Placed Students Feedback
    • Online Training Feedback
    • LinkedIn Profile
    • TechTalk
  • Free YouTube Courses
    • Python for Automation
    • Free QA Video Courses
      • Manual Testing
      • Java For Automation
      • Selenium Webdriver
      • TestNG
      • Cucumber BDD
      • UFT(QTP) Automation
    • Free Data Science Courses
      • Artificial Intelligence for Beginners
      • Python For A.I
      • Python Pandas
      • Python NumPy
      • Mathematics for A.I
  • Home
  • Selenium Java Online Training
  • Self Paced Video Course
    • Selenium Course Curriculum
    • Cypress Course Curriculum
    • Playwright Course Curriculum
  • Tutorials
  • Demo Sites
    • E-Commerce Demo Application
    • Practice Automation
      • Demo Page Healthcare
      • Registration Form
      • Transaction Details
      • DropDown
      • Mouse Event
      • Keyboard Events
      • Alert and Popup
      • Multiple Windows
      • iFrames
      • Wait WebElement
      • WebTable
  • FAQS
  • About Me & Feedback
    • Placed Students Feedback
    • Online Training Feedback
    • LinkedIn Profile
    • TechTalk
  • Free YouTube Courses
    • Python for Automation
    • Free QA Video Courses
      • Manual Testing
      • Java For Automation
      • Selenium Webdriver
      • TestNG
      • Cucumber BDD
      • UFT(QTP) Automation
    • Free Data Science Courses
      • Artificial Intelligence for Beginners
      • Python For A.I
      • Python Pandas
      • Python NumPy
      • Mathematics for A.I

Java For Automation

  • What is Java?
  • Why to Learn Java?
  • History of Java
  • Types of Java Applications
  • Naming Conventions
  • Hello World Program
  • Internal Details of Java program
  • What is Java JDK, JRE and JVM ?
  • What is Java Comments ?
  • What is Java Variable?
  • Variable Naming convention
  • Primitive Data Types
  • Non Primitive Data Types
  • Type Casting
  • Java Arithmetic Operators
  • Relational Operators
  • What is logical operators in Java? 
  • What is Assignment Operators in Java? 
  • What is Unary Operators in Java? 
  • What is Ternary Operators in Java?
  • What is Java Control Statements?
  • What are Decision Making statements?
  • What are looping statements?
  • What are Jump statements?
  • What is OOPS concept in Java?
  • Advantages of Java OOPs
  • What is Java Inheritance?
  • What is Java Constructor?
  • What is this keyword?
  • What is Polymorphism?
  • What is super keyword ?
  • What is final keyword? 
  • What are access modifiers?
  • What is Encapsulation? 
  • What is Abstraction? 
View Categories
  • Home
  • Tutorials
  • Java
  • Java For Automation
  • What is logical operators in Java? 

What is logical operators in Java? 

What is logical operators in Java? 

Logical operators in Java are used to perform logical operations on boolean expressions. These operators return a boolean result (true or false) based on the logical relationship between the operands. 

The main logical operators in Java are:

  1. AND (&&)
  2. OR (||)
  3. NOT (!)

1. AND (&&) #

The && operator returns true if both operands are true. If any one of the operands is false, the result will be false.

Example:

public class LogicalOperatorExample {

    public static void main(String[] args) {

        int a = 10;

        int b = 20;

        /* Check if a is less than 15 
           AND b is greater than 15 */

        if (a < 15 && b > 15) {

            System.out.println("Both conditions are true");

        } else {

            System.out.println("At least one condition is false");

        }

    }

}

Output:
Both conditions are true

In this example, a < 15 is true and b > 15 is also true. Since both conditions are true, the output is “Both conditions are true”.

2. OR (||) #

The || operator returns true if at least one of the operands is true. It only returns false when both operands are false.

Example:

public class LogicalOperatorExample {

    public static void main(String[] args) {

        int a = 10;

        int b = 5;

        // Check if a is greater than 15 OR b is less than 10

        if (a > 15 || b < 10) {

            System.out.println("At least one condition is true");

        } else {

            System.out.println("Both conditions are false");

        }

    }

}

Output:
At least one condition is true

In this example, a > 15 is false, but b < 10 is true. Since one condition is true, the output is “At least one condition is true”.

3. NOT (!) #

The ! operator negates the value of the boolean expression. If the expression is I, it returns false, and if the expression is false, it returns true.

Example:

public class LogicalOperatorExample {

    public static void main(String[] args) {

        boolean condition = false;

        // Check if the condition is NOT true

        if (!condition) {

            System.out.println("Condition is false");

        } else {

            System.out.println("Condition is true");

        }

    }

}

Output:
Condition is false

In this example, condition is false, and the ! operator negates it to true. Hence, the output is “Condition is false”.

Combining Logical Operators #

You can combine logical operators to evaluate complex conditions.

Example:

public class LogicalOperatorExample {

    public static void main(String[] args) {

        int a = 10;

        int b = 20;

        int c = 5;

        /* Check if a is less than 15 AND 
        (b is greater than 15 OR c is greater than 10) */

        if (a < 15 && (b > 15 || c > 10)) {

            System.out.println("Complex condition is true");

        } else {

            System.out.println("Complex condition is false");

        }

    }

}

Output:
Complex condition is true

In this example, the complex condition evaluates to true because a < 15 is true and b > 15 is true (making the whole expression within the parentheses true).

These logical operators are fundamental in controlling the flow of your program by making decisions based on multiple conditions.

Java
What are your Feelings
Share This Article :
  • Facebook
  • X
  • LinkedIn
Relational OperatorsWhat is Assignment Operators in Java? 
Table of Contents
  • 1. AND (&&)
  • 2. OR (||)
  • 3. NOT (!)
  • Combining Logical Operators
© 2018 – 2025 Vinoth Tech Solutions Ltd (UK), Reg. No: 16489105