GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • C programming
  • C++ programming
  • Java programming
  • Python programming
  • HTML
  • CSS
  • Codenora IDE (opens in a new tab)
  • About us
  • Pricing
  • Contact us
  • Privacy policy
  • Terms of service
  • Refund and Cancellation

This view

  • Original page
  • New address

GeezerVison is showing www.dremendo.com — Logical Operators in Java | Dremendo

  • Dremendo
  • C
  • C++
  • Java
  • Python
  • HTML
  • CSS
  • More
    • Home
    • C Tutorial
    • C++ Tutorial
    • Java Tutorial
    • Python Tutorial
    • HTML Tutorial
    • CSS Tutorial
?>
profile image
Announcements
No new announcements.
mentora icon
Java Programming Tutorial
  • Java Basic Concepts
    • Introduction
    • Variable
    • Data Types
    • Naming Conventions
    • Integer Variable
    • Long Variable
    • Short Variable
    • Byte Variable
    • Float and Double Variables
    • Character Variable
    • Boolean Variable
    • Constant Variable
    • Typecasting
  • Operators in Java
    • Operators
    • Assignment Operators
    • Arithmetic Operators
    • Relational Operators
    • Logical Operators
    • Operator Precedence
  • Input and Output in Java
    • Environment Setup
    • Program Basic Structure
    • Output
    • Input
    • Comments
  • Decision Making in Java
    • Decision Making
    • if Statement
    • if else Statement
    • if else if Statement
    • Conditional Operator
    • switch case Statement
  • Mathematical Functions
    • Mathematical Functions
  • Loops in Java
    • Loops
    • for Loop
    • while Loop
    • do while Loop
  • Arrays in Java
    • One Dimensional Array
    • Two Dimensional Array
  • String in Java
    • String
    • StringBuffer
    • StringBuilder
  • Searching in Java
    • Linear Search
    • Binary Search
  • Sorting in Java
    • Bubble Sort
    • Selection Sort
    • Insertion Sort
  • Function in Java
    • Function
    • Recursive Function
    • Function Overloading
  • OOPs in Java
    • Classes and Objects
    • Instance Variables
    • Instance Methods
    • Access Modifiers
    • Constructor
    • Static Variables
    • Static Methods
    • Inheritance
    • Interface
    • Types of Inheritance
    • Polymorphism
    • Final Class and Method
    • Abstract Class and Method
    • Wrapper Classes
  • Data Structures in Java
    • Stack
    • Queue
    • Circular Queue
    • Double Ended Queue
    • Singly Linked List
    • Doubly Linked List
    • Stack using Linked List
    • Queue using Linked List
  • Packages in Java
    • Packages
  • Exception Handling in Java
    • Exception Handling
    • User-Defined Exception
  • Thread in Java
    • Thread
    • Thread Methods
  • File Handling in Java
    • Text File Handling
    • Binary File Handling
    • Random Access File Handling

bookmark
Table of Contents

    Logical Operators in Java

    In this lesson, we will learn what is the Logical Operators and how it works in Java programming with some examples.

    What is Logical Operators

    Logical operators in Java are used to check if given conditions are true or false. They allow a program to make decisions based on multiple conditions. If the result of the logical operation is true, then 1 is returned; otherwise, 0 is returned.

    Video Explanation

    Click the button below to watch the explanation.

    Please wait while the video is loading....
    video image

    There are 3 types of logical operators in Java and they are:

    • AND (&&) Operator
    • OR (||) Operator
    • NOT (!) Operator

    Now let's see the examples of all the logical operators one by one for more understanding.

    AND && Operator

    The AND (&&) operator is used to check if two or more given conditions are true or not. The output of AND (&&) operator is 1 (true) only if all the given conditions are correct otherwise the output will be 0 (false) if any one of the given conditions is wrong.

    Example
    int x=15, y=2, z=6, k=25, a, b;
    
    a = x>5 && y<6 && k>=25;     // output of a is 1
    b = x>y && k<25;             // output of b is 0

    In the example above the output of a is 1 because all the three given conditions x>5, y<6 and k>=25 are correct (true). On the other hand the output of b is 0 because out of the two given conditions x>y and k<25 the second condition k<25 is wrong (false).

    OR || Operator

    The OR (||) operator is used to check if any one of the given conditions is true or not. The output of OR (||) operator is 1 (true) if at least any one of the given conditions is correct otherwise the output will be 0 (false) only if all the given conditions are wrong.

    Example
    int x=15, y=2, z=6, k=25, a, b;
    
    a = x<5 || y<6 || k>=25;     // output of a is 1
    b = x<y || k<25;             // output of b is 0

    In the example above the output of a is 1 because out of the three given conditions x<5, y<6 and k>=25 the second condition is correct (true). On the other hand the output of b is 0 because out of the two given conditions x<y and k<25 both of them are wrong (false).

    NOT ! Operator

    The NOT (!) operator reverse the value of the condition. If the condition is false then the output of NOT (!) operator becomes true (1). If the condition is true then the output becomes false (0).

    Example
    int x=15, y=10, a, b;
    
    a = !(x>5);     // output of a is 0
    b = !(y<5);     // output of b is 1

    In the example above the output of a is 0 because the given condition x>5 is correct (true) so ! (NOT) operator changed the output from 1 to 0. On the other hand the output of b is 1 because the given condition y<5 is wrong (false) so ! (NOT) operator changed the output from 0 to 1.

    Test Your Knowledge

    Attempt the questions in the exercise to check whether you have understood the lesson clearly.

    Exercise
    Test Your Knowledge
    Previous Next

    We care about your privacy

    Dremendo uses cookies and similar technologies that are necessary for the site to work properly, understand how you interact with our website, and measure the effectiveness of our marketing. By clicking Accept All, you consent to the use of non-essential cookies and similar technologies for analytics, advertising, and ad measurement, as described in our Privacy Policy.

    Cookie Preferences

    Choose which types of cookies Dremendo may use.

    Necessary Cookies

    Required for essential website functionality and security.

    Allow
    Analytics Cookies

    Help us understand how visitors use Dremendo.

    Allow
    Marketing Cookies

    Help us measure advertising performance and understand interactions with our advertisements.

    Allow
    Mentora Question Asked: 0 / 20

    Hi, I’m Mentora!

    I’m your personal AI tutor from Dremendo. Ask me anything, and I’ll help you understand it in a simple and easy way.

    You are visitor 01402503 — GeezerVison does not store the pages you look up.
    Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.