Java Operator Fundamentals — Quick & Clear
Short examples and outputs to make operators easy for students.
1. Precedence & Associativity
Operator precedence determines the order in which operators are evaluated. Associativity decides evaluation direction when precedence is same.
Example: multiplication has higher precedence than addition.
2. Assignment Operator ( = )
Assignment stores a value into a variable. Right-hand side is evaluated first, then assigned to the left-hand variable.
3. Basic Arithmetic Operators
Use +, -, *, /, % for arithmetic on numbers.
1
4. Increment (++) and Decrement (--)
Prefix (++x) updates the value then uses it. Postfix (x++) uses the current value then updates it.
6
7
5. Ternary Operator
Short form of if-else: condition ? valueIfTrue : valueIfFalse. Great for concise expressions.
6. Relational Operators
Compare values using >, <, >=, <=, ==, !=. They return boolean results.
7. Boolean Logical Operators
Use && (AND), || (OR), and ! (NOT) to combine boolean expressions.
true
8. Bitwise Logical Operators
Operate at bit-level: &, |, ^, ~, shifts <<, >>. Useful in low-level tasks and optimizations.
7
Mini Precedence Reminder
High to low (short): () → ++/-- → * / % → + - → << >> → & → ^ → | → && → || → ?: → = =
Practice Questions – Click to Reveal Answers
* or +?*) has higher precedence.a = b + 5 do?b + 5 first, then assigns the result to a.x = 5, what is the output of ++x?6 because prefix increments before use.x is positive.String r = (x > 0) ? "Positive" : "Not Positive";
== check for primitives?true && false?false.5 & 3?1.x++ behavior.x, then increments x by 1.&& or &?&& short-circuits (doesn't evaluate right-hand side if left is false); bitwise & always evaluates both sides.Quick Summary
- Precedence & associativity control evaluation order.
- Assignment evaluates RHS then assigns to LHS.
- Use ++/-- carefully — prefix vs postfix matters.
- Ternary is concise for simple conditional values.
- Bitwise operators operate on bits and are useful for low-level tasks.