Logical Operators in Java
Logical operators are used to combine or manipulate boolean values (true or false). They’re
commonly used in conditional statements and loops.
Types of Logical Operators:
Operator Meaning Example Result
&& (Logical Returns true if both conditions are true (5 > 3) && (8 > 6) true
AND)
|| (OR) Returns true if at least one condition is true (5>3) || (8<6) true
! (Logical NOT) Reverses the boolean value !(5 > 3) false
Usage Example:
public class LogicalOperators {
public static void main(String[] args) {
int x = 10, y = 5;
[Link]((x > y) && (y > 0)); // true
[Link]((x < y) || (y > 0)); // true
[Link](!(x > y)); // false
Increment and Decrement Operators
These operators are used to increase or decrease the value of a variable by 1.
Types:
1. Increment (++) → increases value by 1
2. Decrement (--) → decreases value by 1
Each has two forms:
Prefix (++x / --x): The variable is updated first, then used.
Postfix (x++ / x--): The variable is used first, then updated.
Example:
public class IncrementDecrement {
public static void main(String[] args) {
int a = 5;
// Prefix
[Link](++a); // 6 (increment first, then print)
// Postfix
[Link](a++); // 6 (print first, then increment to 7)
// Decrement examples
[Link](--a); // 6 (decrement first, then print)
[Link](a--); // 6 (print first, then decrement to 5)