Java Operators: Arithmetic, Relational, Bitwise, Logical, and More

Arithmetic operators are used in mathematical expressions and behave as they do in algebra. Assuming integer variables x = 10 and y = 20:

Operator Description Example
+ Addition x + y == 30
- Subtraction x - y == -10
* Multiplication x * y == 200
/ Division y / x == 2
% Modulus (remainder after division) y % x == 0
++ Increment by 1 y++ or ++y
-- Decrement by 1 y-- or --y

Increment and Decrement Operators

These unary operators modify a single operand. The placement determines evaluation order:

  • Prefix (++a, --a): Modify first, then use the new value.
  • Postfix (a++, a--): Use the current value first, then modify.
public class IncrementExample {
    public static void main(String[] args) {
        int m = 5;
        int n = ++m; // m becomes 6, then assigned to n
        int p = 5;
        int q = p++; // q gets 5, then p becomes 6
        System.out.println("Prefix result: " + n);  // 6
        System.out.println("Postfix result: " + q); // 5
    }
}

Relational Operators

Used to compare two values, returning a boolean. With x = 10, y = 20:

Operator Description Example
== Equal to (x == y) → false
!= Not equal to (x != y) → true
> Greater than (x > y) → false
< Less than (x < y) → true
>= Greater then or equal to (x >= y) → false
<= Less than or equal to (x <= y) → true

Bitwise Operators

Operate on individual bits of integer types. Given a = 60 (0011 1100) and b = 13 (0000 1101):

Operator Description Example
& AND: 1 only if both bits are 1 a & b = 12 (0000 1100)
` ` OR: 0 only if both bits are 0
^ XOR: 1 if bits differ a ^ b = 49 (0011 0001)
~ NOT: flips all bits ~a = -61 (1100 0011 in two's complement)
<< Left shift a << 2 = 240 (1111 0000)
>> Right shift (sign-extended) a >> 2 = 15 (0000 1111)
>>> Unsigned right shift (zero-fill) a >>> 2 = 15 (0000 1111)

Logical Operators

Work with boolean values. Assuming p = true, q = false:

Operator Description Example
&& Logical AND (short-circuiting) (p && q) → false
` `
! Logical NOT !(p && q) → true

Short-circuit behavior: In a && b, if a is false, b is not evaluated. Similarly, in a || b, if a is true, b is skipped.

Assignment Operators

Combine an operation with assignment:

Operator Equivalent Expression
= c = a + b
+= c = c + a
-= c = c - a
*= c = c * a
/= c = c / a
%= c = c % a
<<= c = c << 2
>>= c = c >> 2
&= c = c & 2
^= c = c ^ 2
` =`

Conditional (Ternary) Operator

Syntax: condition ? valueIfTrue : valueIfFalse. It evaluates a boolean and returns one of two values.

instanceof Operator

Checks if an object is an instance of a specific class or interface:

class Vehicle {}
class Car extends Vehicle {}

public class InstanceCheck {
    public static void main(String[] args) {
        Vehicle v = new Car();
        System.out.println(v instanceof Car);   // true
        System.out.println(v instanceof Vehicle); // true
    }
}

Operator Precedence

When multiple operators appear in an expression, precedenec determines evaluation order. Higher precedance operators are evaluated first. Associativity resolves ties.

Category Operators Associativity
Postfix (), [], . Left to right
Unary expr++, expr-- Left to right
Unary ++expr, --expr, +, -, ~, ! Right to left
Multiplicative *, /, % Left to right
Additive +, - Left to right
Shift <<, >>, >>> Left to right
Relational <, <=, >, >= Left to right
Equality ==, != Left to right
Bitwise AND & Left to right
Bitwise XOR ^ Left to right
Bitwise OR ` `
Logical AND && Left to right
Logical OR `
Conditional ? : Right to left
Assignment =, +=, -= etc. Right to left
Comma , Left to right

Tags: java Operators arithmetic bitwise logical

Posted on Wed, 20 May 2026 20:48:18 +0000 by Guldstrand