Operators in Java (Arithmetic, Relational, Logical)


Table of Contents

  1. Introduction to Operators in Java
  2. Arithmetic Operators
  3. Relational (Comparison) Operators
  4. Logical Operators
  5. Assignment Operators
  6. Unary Operators
  7. Bitwise Operators (Bonus Section)
  8. Operator Precedence and Associativity
  9. Common Mistakes to Avoid
  10. Real-World Use Cases and Examples
  11. Summary and What’s Next

1. Introduction to Operators in Java

In Java, operators are special symbols or keywords used to perform operations on variables and values. They are the backbone of any expression and play a critical role in controlling program logic and data manipulation. Java includes a wide range of operators to support arithmetic calculations, logical decision-making, and even low-level bit manipulation.

Operators work with operands. For example, in the expression a + b, the + operator works on operands a and b.

Operators can be categorized based on their functionality and the number of operands they work with:

  • Unary Operators (one operand)
  • Binary Operators (two operands)
  • Ternary Operator (three operands, covered in the next module)

2. Arithmetic Operators

Arithmetic operators are used to perform basic mathematical operations. They always return numeric results and can be used with all numeric primitive types (int, float, double, etc.).

OperatorDescriptionExampleResult
+Addition10 + 515
-Subtraction10 - 55
*Multiplication10 * 550
/Division10 / 52
%Modulus (remainder)10 % 31

Integer Division vs Floating-point Division

When you divide two integers in Java, the result is also an integer (the decimal part is truncated):

int result = 7 / 2; // result = 3

To get an accurate floating-point result, cast one operand to a float or double:

double result = 7 / 2.0; // result = 3.5

3. Relational (Comparison) Operators

Relational operators compare two values and return a boolean result. These are essential in control flow and decision-making.

OperatorDescriptionExampleResult
==Equal to5 == 5true
!=Not equal to5 != 4true
>Greater than5 > 3true
<Less than5 < 3false
>=Greater than or equal to5 >= 5true
<=Less than or equal to4 <= 5true

These are primarily used in if, while, for, and switch statements.


4. Logical Operators

Logical operators allow combining multiple conditions. They work with boolean values only.

OperatorDescriptionSyntaxExample
&&Logical ANDcondition1 && condition2true && false → false
``Logical OR
!Logical NOT!condition!true → false

Short-circuiting Behavior

Java uses short-circuit evaluation:

  • &&: If the first operand is false, the second operand is not evaluated.
  • ||: If the first operand is true, the second operand is not evaluated.

This behavior improves performance and avoids potential exceptions, such as NullPointerException.


5. Assignment Operators

Assignment operators are used to assign values to variables. Java supports shorthand operators for updating values.

OperatorDescriptionExampleEquivalent
=Assigna = 10—
+=Add and assigna += 5a = a + 5
-=Subtract and assigna -= 3a = a - 3
*=Multiply and assigna *= 2a = a * 2
/=Divide and assigna /= 4a = a / 4
%=Modulus and assigna %= 3a = a % 3

These are helpful in loops and arithmetic-heavy programs.


6. Unary Operators

Unary operators work with a single operand.

OperatorDescriptionExampleResult
+Unary plus (no effect)+aa
-Unary minus-aNegates a
++Increment (prefix/postfix)++a, a++Increments a
--Decrement (prefix/postfix)--a, a--Decrements a
!Logical complement!truefalse

Prefix vs Postfix

int a = 5;
int b = ++a; // b = 6, a = 6 (prefix)
int c = a++; // c = 6, a = 7 (postfix)

7. Bitwise Operators (Bonus Section)

Bitwise operators are used for performing operations at the bit level, primarily useful in low-level programming and optimization.

OperatorDescriptionExample
&Bitwise ANDa & b
``Bitwise OR
^Bitwise XORa ^ b
~Bitwise NOT~a
<<Left shifta << 2
>>Right shifta >> 2

Example:

int x = 5;        // 0101 in binary
int y = 3; // 0011 in binary
System.out.println(x & y); // Output: 1 (0001)

8. Operator Precedence and Associativity

When multiple operators are used in an expression, Java evaluates them based on precedence and associativity.

Precedence (High to Low)

  1. Postfix: expr++, expr--
  2. Unary: ++expr, --expr, +, -, !
  3. Multiplicative: *, /, %
  4. Additive: +, -
  5. Relational: <, <=, >, >=
  6. Equality: ==, !=
  7. Logical AND: &&
  8. Logical OR: ||
  9. Assignment: =, +=, -=, etc.

Associativity

  • Most binary operators are left-to-right associative.
  • Assignment operators are right-to-left associative.

Use parentheses to make expressions clear and override default precedence.


9. Common Mistakes to Avoid

  • Using = instead of == in conditions (if (a = b) instead of if (a == b))
  • Forgetting short-circuit behavior in && and ||
  • Integer division errors due to type mismatch
  • Using post-increment when pre-increment is needed (or vice versa)
  • Applying logical operators to non-boolean operands

10. Real-World Use Cases and Examples

  • Use relational and logical operators for input validation: if (age >= 18 && citizenship.equals("Indian")) { System.out.println("Eligible to vote."); }
  • Arithmetic operators in billing applications: double total = quantity * pricePerUnit + tax;
  • Bitwise operations in graphics and cryptography
  • Assignment operators in loops to update counters

11. Summary and What’s Next

In this module, you have covered:

  • Different categories of Java operators
  • Syntax and examples for arithmetic, relational, logical, and assignment operators
  • Operator precedence and associativity
  • Common mistakes and real-world use cases

Understanding how and when to use each type of operator helps you write cleaner and more efficient code.