Java Tutorial
Operators
Flow Control
String
Number and Date
Built-in Classes
Array
Class and Object
Inheritance and Polymorphism
Exception Handling
Collections, Generics and Enumerations
Reflection
Input/Output Stream
Annotation
Logical operators are used to perform logical operations on boolean expressions. They allow you to combine, negate, or compare boolean values in various ways. Java provides three main logical operators: AND (&&), OR (||), and NOT (!). This tutorial will cover the usage and behavior of these logical operators in Java.
The logical AND operator (&&) returns true
if both operands are true; otherwise, it returns false
.
boolean result = true && true; // result is true result = true && false; // result is false result = false && true; // result is false result = false && false; // result is false
Note that the logical AND operator is short-circuiting: if the first operand is false, the second operand is not evaluated, as the result is guaranteed to be false.
The logical OR operator (||) returns true
if at least one of the operands is true; otherwise, it returns false
.
boolean result = true || true; // result is true result = true || false; // result is true result = false || true; // result is true result = false || false; // result is false
Similar to the logical AND operator, the logical OR operator is also short-circuiting: if the first operand is true, the second operand is not evaluated, as the result is guaranteed to be true.
The logical NOT operator (!) returns true
if the operand is false and false
if the operand is true. In other words, it negates the boolean value of the operand.
boolean result = !true; // result is false result = !false; // result is true
You can combine logical operators to create more complex boolean expressions. Parentheses can be used to control the order of evaluation.
boolean a = true; boolean b = false; boolean c = true; boolean result = a && b || c; // result is true result = a && (b || c); // result is true result = (a && b) || c; // result is true result = !(a && (b || c)); // result is false
This tutorial covered the basics of logical operators in Java, including the logical AND (&&), logical OR (||), and logical NOT (!) operators. These operators allow you to combine, negate, or compare boolean values in your code, enabling you to create complex boolean expressions and implement conditional logic.
Using && (logical AND) in Java:
&&
operator in Java is used for logical AND. It returns true if both operands are true, otherwise false.boolean condition1 = true; boolean condition2 = false; boolean result = condition1 && condition2; // result is false
|| (logical OR) operator in Java examples:
||
operator performs logical OR. It returns true if at least one operand is true.boolean condition1 = true; boolean condition2 = false; boolean result = condition1 || condition2; // result is true
! (logical NOT) operator usage in Java:
!
operator negates the boolean value. It returns true if the operand is false, and vice versa.boolean condition = true; boolean result = !condition; // result is false
Combining logical operators in Java expressions:
boolean condition1 = true; boolean condition2 = false; boolean condition3 = true; boolean result = condition1 && (condition2 || condition3); // result is true
Short-circuiting behavior of && and || in Java:
&&
and ||
operators exhibit short-circuiting behavior. If the result can be determined by evaluating only the first operand, the second operand is not evaluated.boolean condition1 = false; boolean condition2 = true; boolean result = condition1 || (condition2); // condition2 is not evaluated
Complex conditions with nested logical operators in Java:
int number = 5; boolean result = (number > 0 && number < 10) || (number < -5); // result is true if the number is between 0 and 10 (exclusive) or less than -5