Java Logical Operators with Examples
Last Updated :
16 Apr, 2025
Logical operators are used to perform logical “AND”, “OR“, and “NOT” operations, i.e., the functions similar to AND gate and OR gate in digital electronics. They are used to combine two or more conditions/constraints or to complement the evaluation of the original condition under particular consideration. One thing to keep in mind is that, while using the AND operator, the second condition is not evaluated if the first one is false. While using the OR operator, the second condition is not evaluated if the first one is true, i.e., the AND and OR operators have a short-circuiting effect. Used extensively to test for several conditions for making a decision.
- AND Operator (&&): If( a && b ) [if true execute else don’t]
- OR Operator (||): If( a || b) [if one of them is true to execute; else don’t]
- NOT Operator (!): !(a<b) [returns false if a is smaller than b]
Example of Logical Operators in Java
Here is an example depicting all the operators where the values of variables a, b, and c are kept the same for all the situations.
a = 10, b = 20, c = 30
For AND operator:
Condition 1: c > a
Condition 2: c > b
Output:
True [Both Conditions are true]
For OR Operator:
Condition 1: c > a
Condition 2: c > b
Output:
True [One of the Condition if true]
For NOT Operator:
Condition 1: c > a
Condition 2: c > b
Output:
False [Because the result was true and NOT operator did it’s opposite]
Logical AND Operator (&&) with Example
This operator returns true when both the conditions under consideration are satisfied or are true. If even one of the two yields false, the operator results false. In Simple terms, cond1 && cond2 returns true when both cond1 and cond2 are true (i.e. non-zero).
Syntax:
condition1 && condition2
Illustration:
a = 10, b = 20, c = 20
condition1: a < b
condition2: b == c
if(condition1 && condition2)
d = a + b + c
// Since both the conditions are true
d = 50.
Example:
Java
// Java code to illustrate
// logical AND operator
import java.io.*;
class Geeks {
public static void main(String[] args)
{
// initializing variables
int a = 10, b = 20, c = 20, d = 0;
// Displaying a, b, c
System.out.println("Var1 = " + a);
System.out.println("Var2 = " + b);
System.out.println("Var3 = " + c);
// using logical AND to verify
// two constraints
if ((a < b) && (b == c)) {
d = a + b + c;
System.out.println("The sum is: " + d);
}
else
System.out.println("False conditions");
}
}
OutputVar1 = 10
Var2 = 20
Var3 = 20
The sum is: 50
Short-Circuiting in AND
Now in the below example, we can see the short-circuiting effect. Here, when the execution reaches to if statement, the first condition inside the if statement is false and so the second condition is never checked. Thus the ++b(pre-increment of b) never happens, and b remains unchanged.
Example:
Java
import java.io.*;
class Geeks {
public static void main(String[] args)
{
// initializing variables
int a = 10, b = 20, c = 15;
// displaying b
System.out.println("Value of b: " + b);
// Using logical AND
// Short-Circuiting effect as the first condition is
// false so the second condition is never reached
// and so ++b(pre increment) doesn't take place and
// value of b remains unchanged
if ((a > c) && (++b > c)) {
System.out.println("Inside if block");
}
// displaying b
System.out.println("Value of b: " + b);
}
}
OutputValue of b: 20
Value of b: 20
Output:

Logical OR Operator (||) with Example
This operator returns true when one of the two conditions under consideration is satisfied or is true. If even one of the two yields true, the operator results true. To make the result false, both the constraints need to return false.
Syntax:
condition1 || condition2
Example:
a = 10, b = 20, c = 20
condition1: a < b
condition2: b > c
if(condition1 || condition2)
d = a + b + c
// Since one of the condition is true
d = 50.
Illustration:
Java
// Java code to illustrate
// logical OR operator
import java.io.*;
class Geeks {
public static void main(String[] args)
{
// initializing variables
int a = 10, b = 1, c = 10, d = 30;
// Displaying a, b, c
System.out.println("Var1 = " + a);
System.out.println("Var2 = " + b);
System.out.println("Var3 = " + c);
System.out.println("Var4 = " + d);
// using logical OR to verify
// two constraints
if (a > b || c == d)
System.out.println(
"One or both + the conditions are true");
else
System.out.println(
"Both the + conditions are false");
}
}
OutputVar1 = 10
Var2 = 1
Var3 = 10
Var4 = 30
One or both + the conditions are true
Short-Circuiting in OR
Now in the below example, we can see the short-circuiting effect for OR operator. Here, when the execution reaches to if statement, the first condition inside the if statement is true and so the second condition is never checked. Thus the ++b (pre-increment of b) never happens, and b remains unchanged.
Example:
Java
import java.io.*;
class Geeks {
public static void main (String[] args) {
// initializing variables
int a = 10, b = 20, c = 15;
// displaying b
System.out.println("Value of b: " +b);
// Using logical OR
// Short-circuiting effect as the first condition is true
// so the second condition is never reached
// and so ++b (pre-increment) doesn't take place and
// value of b remains unchanged
if((a < c) || (++b < c))
System.out.println("Inside if");
// displaying b
System.out.println("Value of b: " +b);
}
}
OutputValue of b: 20
Inside if
Value of b: 20
Logical NOT Operator (!) with Example
Unlike the previous two, this is a unary operator and returns true when the condition under consideration is not satisfied or is a false condition. Basically, if the condition is false, the operation returns true and when the condition is true, the operation returns false.
Syntax:
!(condition)
Example:
a = 10, b = 20
!(a<b) // returns false
!(a>b) // returns true
Illustartion:
Java
// Java code to illustrate
// logical NOT operator
import java.io.*;
class Geeks {
public static void main(String[] args)
{
// initializing variables
int a = 10, b = 1;
// Displaying a, b, c
System.out.println("Var1 = " + a);
System.out.println("Var2 = " + b);
// Using logical NOT operator
System.out.println("!(a < b) = " + !(a < b));
System.out.println("!(a > b) = " + !(a > b));
}
}
OutputVar1 = 10
Var2 = 1
!(a < b) = true
!(a > b) = false
Using Logical Operators with Boolean Values
Syntax:
boolean a = true;
boolean b = false;
Example:
Java
public class Geeks {
public static void main(String[] args) {
boolean a = true;
boolean b = false;
System.out.println("a: " + a);
System.out.println("b: " + b);
System.out.println("a && b: " + (a && b));
System.out.println("a || b: " + (a || b));
System.out.println("!a: " + !a);
System.out.println("!b: " + !b);
}
}
Outputa: true
b: false
a && b: false
a || b: true
!a: false
!b: true
Explanation: The above example demonstrates the use of logical operator with two boolean variables. The AND (&&) operator returns true if both the variable a and b are true, otherwise it returns false. The OR (||) operator return true if either a and b is true otherwise it return false. The NOT (!) operator reverse the value of the operand. It means !a is false and !b is true.
Advantages
The advantages of logical operators are listed below:
- Short-circuit evaluation: One of the main advantages of logical operators is that they support short-circuit evaluation. It means if the first condition is correct stop checking for the second conditon. It reduce the unnecessary computation and also optimize the performance.
- Readability: Logical operator makes the mode more easy to understand.
- Flexibility: Logical operator allows combining various operator in different ways to handle complex situation.
- Reusability: With the help of logical operator, we can use the same logic on different parts of the program.
- Debugging: With the help of logical operator, the debugging become easier.
Disadvantages
The Disadvantages of logical operators are listed below:
- Limited expressiveness: Logical operators are not good for complex conditions, where we need to check mutliple conditions in a specific order. If-else statements are better choice in such conditions.
- Potential for confusion: Sometimes logical operatos are very confusing. It is required to use paranthese to show the order of checking the conditions.
- Boolean coercion: Logical operator can also cause unexpected result, if they are used with values that are not true or false.
Overall, logical operators are an important tool for developers and play a crucial role in the implementation of complex conditions in a program. They help to improve the readability, flexibility, reusability, and debuggability of the code.
Similar Reads
Basics of Java
If you are new to the world of coding and want to start your coding journey with Java, then this learn Java a beginners guide gives you a complete overview of how to start Java programming. Java is among the most popular and widely used programming languages and platforms. A platform is an environme
10 min read
Java is a class-based, object-oriented programming language that is designed to have as few implementation dependencies as possible. It is intended to let application developers Write Once and Run Anywhere (WORA), meaning that compiled Java code can run on all platforms that support Java without the
9 min read
Nowadays Java and C++ programming languages are vastly used in competitive coding. Due to some awesome features, these two programming languages are widely used in industries as well as comepetitive programming . C++ is a widely popular language among coders for its efficiency, high speed, and dynam
6 min read
In the journey to learning the Java programming language, setting up environment variables for Java is essential because it helps the system locate the Java tools needed to run the Java programs. Now, this guide on how to setting up environment variables for Java is a one-place solution for Mac, Win
5 min read
Java is an object-oriented programming language that is known for its simplicity, portability, and robustness. The syntax of Java programming language is very closely aligned with C and C++, which makes it easier to understand. Java Syntax refers to a set of rules that defines how Java programs are
6 min read
Java is one of the most popular and widely used programming languages and platforms. In this article, we will learn how to write a simple Java Program. This article will guide you on how to write, compile, and run your first Java program. With the help of Java, we can develop web and mobile applicat
6 min read
Understanding the difference between JDK, JRE, and JVM plays a very important role in understanding how Java works and how each component contributes to the development and execution of Java applications. The main difference between JDK, JRE, and JVM is: JDK: Java Development Kit is a software devel
4 min read
JVM(Java Virtual Machine) runs Java applications as a run-time engine. JVM is the one that calls the main method present in a Java code. JVM is a part of JRE(Java Runtime Environment). Java applications are called WORA (Write Once Run Anywhere). This means a programmer can develop Java code on one s
7 min read
An identifier in Java is the name given to Variables, Classes, Methods, Packages, Interfaces, etc. These are the unique names used to identify programming elements. Every Java Variable must be identified with a unique name. Example: public class Test{ public static void main(String[] args) { int a =
2 min read
Variables & DataTypes in Java
In Java, variables are containers that store data in memory. Understanding variables plays a very important role as it defines how data is stored, accessed, and manipulated. Key Components of Variables in Java: A variable in Java has three components, which are listed below: Data Type: Defines the k
9 min read
The scope of variables is the part of the program where the variable is accessible. Like C/C++, in Java, all identifiers are lexically (or statically) scoped, i.e. scope of a variable can be determined at compile time and independent of the function call stack. In this article, we will learn about J
6 min read
Java is statically typed and also a strongly typed language because each type of data (such as integer, character, hexadecimal, packed decimal, and so forth) is predefined as part of the programming language and all constants or variables defined for a given program must be declared with the specifi
15 min read
Operators in Java
Java operators are special symbols that perform operations on variables or values. These operators are essential in programming as they allow you to manipulate data efficiently. They can be classified into different categories based on their functionality. In this article, we will explore different
15 min read
Operators constitute the basic building block to any programming language. Java too provides many types of operators which can be used according to the need to perform various calculations and functions, be it logical, arithmetic, relational, etc. They are classified based on the functionality they
6 min read
Operators constitute the basic building block of any programming language. Java too provides many types of operators which can be used according to the need to perform various calculations and functions, be it logical, arithmetic, relational, etc. They are classified based on the functionality they
7 min read
Operators constitute the basic building block to any programming language. Java too provides many types of operators which can be used according to the need to perform various calculations and functions be it logical, arithmetic, relational, etc. They are classified based on the functionality they p
8 min read
Operators constitute the basic building block to any programming language. Java too provides many types of operators which can be used according to the need to perform various calculations and functions, be it logical, arithmetic, relational, etc. They are classified based on the functionality they
10 min read
Logical operators are used to perform logical "AND", "OR", and "NOT" operations, i.e., the functions similar to AND gate and OR gate in digital electronics. They are used to combine two or more conditions/constraints or to complement the evaluation of the original condition under particular consider
8 min read
Operators constitute the basic building block of any programming language. Java provides many types of operators that can be used according to the need to perform various calculations and functions, be it logical, arithmetic, relational, etc. They are classified based on the functionality they provi
5 min read
In Java, Operators are special symbols that perform specific operations on one or more than one operands. They build the foundation for any type of calculation or logic in programming. There are so many operators in Java, among all, bitwise operators are used to perform operations at the bit level.
6 min read
Arrays in Java
Arrays in Java are one of the most fundamental data structures that allow us to store multiple values of the same type in a single variable. They are useful for storing and managing collections of data. Arrays in Java are objects, which makes them work differently from arrays in C/C++ in terms of me
15+ min read
Multidimensional arrays are used to store the data in rows and columns, where each row can represent another individual array are multidimensional array. It is also known as array of arrays. The multidimensional array has more than one dimension, where each row is stored in the heap independently. T
10 min read
In Java, Jagged array is an array of arrays such that member arrays can be of different sizes, i.e., we can create a 2-D array but with a variable number of columns in each row. Example: arr [][]= { {1,2}, {3,4,5,6},{7,8,9}}; So, here you can check that the number of columns in row1!=row2!=row3. Tha
5 min read
Strings in Java
In Java, a String is the type of object that can store a sequence of characters enclosed by double quotes, and every character is stored in 16 bits, i.e., using UTF 16-bit encoding. A string acts the same as an array of characters. Java provides a robust and flexible API for handling strings, allowi
10 min read
String is a sequence of characters. In Java, objects of the String class are immutable which means they cannot be changed once created. In this article, we will learn about the String class in Java. Example of String Class in Java: [GFGTABS] Java // Java Program to Create a String import java.io.*;
7 min read
The StringBuffer class in Java represents a sequence of characters that can be modified, which means we can change the content of the StringBuffer without creating a new object every time. It represents a mutable sequence of characters. Features of StringBuffer ClassThe key features of StringBuffer
11 min read
In Java, the StringBuilder class is a part of the java.lang package that provides a mutable sequence of characters. Unlike String (which is immutable), StringBuilder allows in-place modifications, making it memory-efficient and faster for frequent string operations. Declaration: StringBuilder sb = n
7 min read
OOPS in Java
Java Object-Oriented Programming (OOPs) is a fundamental concept in Java that every developer must understand. It allows developers to structure code using classes and objects, making it more modular, reusable, and scalable. The core idea of OOPs is to bind data and the functions that operate on it,
13 min read
In Java, classes and objects are basic concepts of Object Oriented Programming (OOPs) that are used to represent real-world concepts and entities. The class represents a group of objects having similar properties and behavior, or in other words, we can say that a class is a blueprint for objects, wh
12 min read
Java Methods are blocks of code that perform a specific task. A method allows us to reuse code, improving both efficiency and organization. All methods in Java must belong to a class. Methods are similar to functions and expose the behavior of objects. Example: Java program to demonstrate how to cre
8 min read
In Java, access modifiers are essential tools that define how the members of a class, like variables, methods, and even the class itself can be accessed from other parts of our program. They are an important part of building secure and modular code when designing large applications. Understanding de
7 min read
A Wrapper class in Java is one whose object wraps or contains primitive data types. When we create an object in a wrapper class, it contains a field, and in this field, we can store primitive data types. In other words, we can wrap a primitive value into a wrapper class object. Let's check on the wr
6 min read
Firstly the question that hits the programmers is when we have primitive data types then why does there arise a need for the concept of wrapper classes in java. It is because of the additional features being there in the Wrapper class over the primitive data types when it comes to usage. These metho
3 min read