Operators · Lesson 9 of 42
Relational, Logical, Bitwise and Ternary Operators
Every decision in a program – is the password valid, is the user an admin, is the cart empty – comes down to an expression that is true or false. This lesson covers the operators that produce and combine those booleans, plus the bitwise operators that power permissions, flags and many interview puzzles.
In this lesson
- Relational operators and
==vsequals() &&,||,!and short-circuit evaluation- Bitwise and shift operators
- The ternary operator
Relational operators
public class Main {
public static void main(String[] args) {
int marks = 72;
System.out.println(marks > 60); // true
System.out.println(marks <= 70); // false
System.out.println(marks == 72); // true
System.out.println(marks != 72); // false
System.out.println('a' < 'b'); // chars compare by code
System.out.println(10 == 10.0); // int promoted to double
}
}
true false true false true true
== compares references for objects
public class Main {
public static void main(String[] args) {
String a = "java";
String b = "java"; // same pooled literal
String c = new String("java"); // a new object
System.out.println(a == b);
System.out.println(a == c);
System.out.println(a.equals(c));
System.out.println("Java".equalsIgnoreCase(c));
}
}
true false true true
Common mistake: comparing strings with ==
== asks "is this the same object?", not "is the text the same?". It may appear to work with literals because of the string pool, then fail with strings read from input or a database. Always use .equals() – or Objects.equals(a, b) when either side may be null.
Logical operators
| Operator | Meaning | True when… |
|---|---|---|
&& | AND | both sides are true |
|| | OR | at least one side is true |
! | NOT | the operand is false |
public class Main {
public static void main(String[] args) {
int age = 22;
boolean hasId = true, banned = false;
System.out.println(age >= 18 && hasId && !banned);
boolean weekend = false, holiday = true;
System.out.println(weekend || holiday);
int year = 2028;
boolean leap = (year % 4 == 0 && year % 100 != 0) || year % 400 == 0;
System.out.println(year + " leap: " + leap);
}
}
true true 2028 leap: true
Short-circuit evaluation
&& stops as soon as the left side is false; || stops as soon as it is true. The right side is never evaluated – which prevents crashes:
name != null && name.length() > 3
- name != null?evaluate the left side
- false → stopresult is false; name.length() is never called
- true → continueevaluate name.length() > 3 safely
public class Main {
public static void main(String[] args) {
String name = null;
if (name != null && name.length() > 3) {
System.out.println("long name");
} else {
System.out.println("no name or too short");
}
int calls = 0;
boolean r = true || (++calls > 0); // right side skipped
System.out.println(r + " calls=" + calls);
}
}
no name or too short true calls=0
The single & and | also work on booleans but always evaluate both sides. Use them only when you deliberately need the side effect – which is rare.
Bitwise and shift operators
These work on the individual bits of integers.
| Operator | Name | Example (a = 12 = 1100, b = 10 = 1010) | Result |
|---|---|---|---|
& | AND | a & b → 1000 | 8 |
| | OR | a | b → 1110 | 14 |
^ | XOR | a ^ b → 0110 | 6 |
~ | NOT | ~a | −13 |
<< | left shift | a << 2 (× 4) | 48 |
>> | signed right shift | a >> 2 (÷ 4) | 3 |
>>> | unsigned right shift | -16 >>> 28 | 15 |
public class Main {
public static void main(String[] args) {
int a = 12, b = 10;
System.out.println((a & b) + " " + (a | b) + " " + (a ^ b) + " " + (~a));
System.out.println((a << 2) + " " + (a >> 2) + " " + (-16 >> 2) + " " + (-16 >>> 28));
int n = 37;
System.out.println(n + " is " + ((n & 1) == 0 ? "even" : "odd"));
System.out.println("power of two? " + (64 > 0 && (64 & (64 - 1)) == 0));
System.out.println("set bits in 13: " + Integer.bitCount(13));
}
}
8 14 6 -13 48 3 -4 15 37 is odd power of two? true set bits in 13: 3
Real use: permission flags
public class Main {
static final int READ = 1, WRITE = 2, DELETE = 4; // 001, 010, 100
public static void main(String[] args) {
int perms = READ | WRITE; // grant read + write
System.out.println("can write: " + ((perms & WRITE) != 0));
System.out.println("can delete: " + ((perms & DELETE) != 0));
perms |= DELETE; // add delete
perms &= ~WRITE; // remove write
System.out.println("perms = " + Integer.toBinaryString(perms));
}
}
can write: true can delete: false perms = 101
The ternary operator
condition ? valueIfTrue : valueIfFalse picks one of two values in a single expression:
public class Main {
public static void main(String[] args) {
int marks = 58;
String result = marks >= 40 ? "Pass" : "Fail";
System.out.println(result);
int items = 1;
System.out.println(items + " item" + (items == 1 ? "" : "s"));
int a = 14, b = 31, c = 9;
int max = a > b ? (a > c ? a : c) : (b > c ? b : c);
System.out.println("max = " + max);
}
}
Pass 1 item max = 31
Nested ternaries like the max line are popular in tests but hard to read in real code – prefer Math.max(a, Math.max(b, c)) or an if.
Practice
Try it yourself
Swap two numbers without a temporary variable using XOR.
Show solution
public class Main { public static void main(String[] args) { int x = 7, y = 12; x ^= y; y ^= x; x ^= y; System.out.println(x + " " + y); } }12 7
Check whether a password has at least 8 characters and is not "password".
Show solution
public class Main { public static void main(String[] args) { String pwd = "learn2026!"; boolean ok = pwd.length() >= 8 && !pwd.equalsIgnoreCase("password"); System.out.println(ok); } }true
Quick check
How should you compare two strings for equal text?
With a.equals(b) (or Objects.equals(a, b) if either may be null), not ==.
What is short-circuit evaluation?
&& skips the right side when the left is false, and || skips it when the left is true.
What is 5 & 3? And 5 ^ 3?
1 (101 & 011) and 6 (101 ^ 011).
Summary
- Relational operators return booleans; use
equals()for object contents. &&and||short-circuit – put null checks on the left.- Bitwise operators manipulate bits (flags, even/odd, powers of two); the ternary picks between two values.
Finished this lesson?
Mark it complete to track your progress, then carry on with the next lesson.