Classes/Objects  «Prev  Next»


Lesson 2 Java operators
Objective Summarize Java’s operators with clear rules, runnable examples, and common pitfalls.

Java Operators: A Practical Summary

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.

Arithmetic operators

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+11
- (unary)Negation-xFlips sign of x
+ (binary)Addition2 + 24
- (binary)Subtraction3 - 21
*Multiplication4 * 416
/Division (truncates toward 0 for integers)19 / 44
%Remainder19 % 43
+ (String)Concatenation"abc" + "def""abcdef"

Prefix vs. postfix

++x increments then yields the new value; x++ yields current value then increments. Same idea for --.

Bitwise and shift operators

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)
&AND3 & 5100000011 & 00000101 = 00000001
|OR3 | 4700000011 | 00000100 = 00000111
^XOR5 ^ 4100000101 ^ 00000100 = 00000001
<<Left shift3 << 32400000011 << 3 = 00011000
>>Right shift (sign-extending)-6 >> 2-2keeps sign bits
>>>Right shift (zero-extending)6 >>> 21fills with zeros

Bitwise demo

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
  }
}

Boolean logical operators

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).

Outcome of using boolean literal values with the logical operators AND, OR, and NOT
Outcome table for AND (&&), 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


Comparison and type-test operators

Relational: <, <=, >, >=. Equality: ==, !=. Type test: instanceof.

OperatorDescriptionExampleResult
<Less than1 < 2true
<=Less than or equal1 <= 1true
>Greater than1 > 2false
>=Greater than or equal1 >= 1true
==Equal to1 == 2false
!=Not equal to1 != 2true
instanceofType test / pattern match"xyz" instanceof Stringtrue

Assignment and compound assignment

Simple assignment = plus compound forms: +=, -=, *=, /=, %=, &=, |=, ^=, <<=, >>=, >>>=.

Ternary (conditional) operator

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";

Cast operator and conversions

(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


Precedence & associativity (abridged)

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.

Quick check (reads as true/false)

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; }

Common pitfalls


SEMrush Software