Getting Started

Operators

Master arithmetic, comparison, logical, and bitwise operators in Java.

Arithmetic Operators

Standard math operations: +, -, *, /, % (modulo).

Note: Integer division truncates the result. Use double or cast to get decimals.

Comparison Operators

Return a boolean: ==, !=, <, >, <=, >=.

Important: Use .equals() to compare String values, not ==. == checks object reference, not content.

Logical Operators

  • && — AND (short-circuit)
  • || — OR (short-circuit)
  • ! — NOT

Increment / Decrement

  • ++x — pre-increment (increment then use)
  • x++ — post-increment (use then increment)

Example

java
// Arithmetic
int a = 10, b = 3;
System.out.println(a + b);   // 13
System.out.println(a - b);   // 7
System.out.println(a * b);   // 30
System.out.println(a / b);   // 3 (integer division)
System.out.println(a % b);   // 1 (remainder)
System.out.println((double) a / b); // 3.333...

// Comparison
System.out.println(a > b);   // true
System.out.println(a == b);  // false

// String comparison (use .equals!)
String s1 = "hello";
String s2 = "hello";
System.out.println(s1 == s2);       // may be true/false (reference)
System.out.println(s1.equals(s2));  // true (content)

// Logical
boolean x = true, y = false;
System.out.println(x && y);  // false
System.out.println(x || y);  // true
System.out.println(!x);      // false

// Increment/decrement
int n = 5;
System.out.println(n++); // 5 (post-increment)
System.out.println(n);   // 6
System.out.println(++n); // 7 (pre-increment)

// Compound assignment
n += 10;  // n = n + 10
n *= 2;   // n = n * 2
Try it yourself — JAVA