| Lesson 2 | Java operators |
| Objective | Summarize Java’s operators with clear rules, runnable examples, and common pitfalls. |
This page consolidates arithmetic, bitwise/shift, logical, comparison, assignment, ternary, and cast operators into a single, cohesive reference. Examples compile on modern Java (LTS or feature releases) and highlight gotchas such as short-circuiting, integer division, numeric promotion, and reference equality vs. value equality.
Unary: + (no-op sign), - (negation), ++, --. Binary: +, -, *, /, %. With integers, division truncates toward zero; remainder (%) keeps the dividend’s sign. The + operator also concatenates String values.
| Operator | Description | Example | Result |
| + (unary) | Sign identification | +1 | 1 |
| - (unary) | Negation | -x | Flips sign of x |
| + (binary) | Addition | 2 + 2 | 4 |
| - (binary) | Subtraction | 3 - 2 | 1 |
| * | Multiplication | 4 * 4 | 16 |
| / | Division (truncates toward 0 for integers) | 19 / 4 | 4 |
| % | Remainder | 19 % 4 | 3 |
| + (String) | Concatenation | "abc" + "def" | "abcdef" |
++x increments then yields the new value; x++ yields current value then increments. Same idea for --.
Operate on integer types only: ~ (bitwise NOT), & (AND), | (OR), ^ (XOR), shifts <<, >> (arithmetic right, sign-extending), and >>> (logical right, zero-extending).
| Op | Description | Example | Result | Computation |
| ~ | Bitwise complement | ~3 | -4 | ~00000011 = 11111100 (two’s complement) |
| & | AND | 3 & 5 | 1 | 00000011 & 00000101 = 00000001 |
| | | OR | 3 | 4 | 7 | 00000011 | 00000100 = 00000111 |
| ^ | XOR | 5 ^ 4 | 1 | 00000101 ^ 00000100 = 00000001 |
| << | Left shift | 3 << 3 | 24 | 00000011 << 3 = 00011000 |
| >> | Right shift (sign-extending) | -6 >> 2 | -2 | keeps sign bits |
| >>> | Right shift (zero-extending) | 6 >>> 2 | 1 | fills with zeros |
public class BitwiseDemo {
public static void main(String[] args) {
System.out.println(~3); // -4
System.out.println(3 & 5); // 1
System.out.println(3 | 4); // 7
System.out.println(5 ^ 4); // 1
System.out.println(3 << 3); // 24
System.out.println(-6 >> 2); // -2
System.out.println(6 >>> 2); // 1
}
}
These operate only on boolean: non-short-circuit &, |, ^, logical NOT !, and short-circuit &&, ||. Prefer short-circuiting in conditionals to avoid needless evaluation (and side effects).
&&), OR (||), and NOT (!) using boolean literals.int a = 10, b = 20;
System.out.println(a > 20 && b > 10); // false (first term false)
System.out.println(a > 20 || b > 10); // true (second term true)
System.out.println(!(b > 10)); // false
System.out.println(!(a > 20)); // true
Relational: <, <=, >, >=. Equality: ==, !=. Type test: instanceof.
==/!= compare values.==/!= compare object identity. Use equals() for value equality (e.g., String).if (obj instanceof String s) { ... } safely narrows the type.| Operator | Description | Example | Result |
| < | Less than | 1 < 2 | true |
| <= | Less than or equal | 1 <= 1 | true |
| > | Greater than | 1 > 2 | false |
| >= | Greater than or equal | 1 >= 1 | true |
| == | Equal to | 1 == 2 | false |
| != | Not equal to | 1 != 2 | true |
| instanceof | Type test / pattern match | "xyz" instanceof String | true |
Simple assignment = plus compound forms: +=, -=, *=, /=, %=, &=, |=, ^=, <<=, >>=, >>>=.
short s = 1; s += 1; compiles (with implicit cast). s = s + 1; requires an explicit cast.Form: condition ? whenTrue : whenFalse. Result types must be compatible or one convertible to the other.
int x = 7;
String parity = (x % 2 == 0) ? "even" : "odd";
(type) performs explicit conversion. Widening (e.g., int → long) is safe; narrowing (e.g., int → short) may overflow/truncate.
long big = 3_000_000_000L;
int narrowed = (int) big; // overflow: value wraps per two’s complement
From high → low: (), unary (+ - ! ~ ++ --), multiplicative (* / %), additive (+ -), shifts, relational (< <= > >= instanceof), equality (== !=), bitwise AND &, XOR ^, OR |, logical AND &&, logical OR ||, ternary ? :, assignment.
Use parentheses to document intent and avoid precedence mistakes.
System.out.println( (3 / 2) * 2 == 3 ); // false (integer truncation)
System.out.println( "a" + 1 + 2 ); // "a12" (left-to-right, String first)
System.out.println( 1 + 2 + "a" ); // "3a" (numbers first, then String)
System.out.println( (null instanceof Object) ); // false
System.out.println( true & f()); // f() called (non-short-circuit)
System.out.println( true && f()); // f() called; left is true, must check right
static boolean f() { System.out.println("side-effect"); return true; }
== vs equals(): use equals() for value equality of objects (e.g., String).&&/|| over &/| in boolean logic to avoid unintended side effects.double/BigDecimal when appropriate.>> sign-extends, >>> zero-extends.