Start Coding

Topics

Java Operators

Java operators are special symbols that perform specific operations on one, two, or three operands, and then return a result. They are essential for manipulating variables and values in Java programs.

Types of Java Operators

1. Arithmetic Operators

Arithmetic operators perform basic mathematical operations.

  • Addition (+)
  • Subtraction (-)
  • Multiplication (*)
  • Division (/)
  • Modulus (%)

Example:


int a = 10, b = 3;
int sum = a + b;  // 13
int difference = a - b;  // 7
int product = a * b;  // 30
int quotient = a / b;  // 3
int remainder = a % b;  // 1
    

2. Assignment Operators

Assignment operators are used to assign values to variables.

  • Simple assignment (=)
  • Addition assignment (+=)
  • Subtraction assignment (-=)
  • Multiplication assignment (*=)
  • Division assignment (/=)
  • Modulus assignment (%=)

Example:


int x = 5;
x += 3;  // x is now 8
x *= 2;  // x is now 16
    

3. Comparison Operators

Comparison operators are used to compare two values.

  • Equal to (==)
  • Not equal to (!=)
  • Greater than (>)
  • Less than (<)
  • Greater than or equal to (>=)
  • Less than or equal to (<=)

Example:


int a = 5, b = 7;
boolean isEqual = (a == b);  // false
boolean isGreater = (a > b);  // false
    

4. Logical Operators

Logical operators are used to determine the logic between variables or values.

  • Logical AND (&&)
  • Logical OR (||)
  • Logical NOT (!)

Example:


boolean x = true, y = false;
boolean result = x && y;  // false
result = x || y;  // true
result = !x;  // false
    

5. Bitwise Operators

Bitwise operators perform operations on individual bits of integer types.

  • Bitwise AND (&)
  • Bitwise OR (|)
  • Bitwise XOR (^)
  • Bitwise NOT (~)
  • Left shift (<<)
  • Right shift (>>)
  • Unsigned right shift (>>>)

Example:


int a = 5, b = 3;
int result = a & b;  // 1
result = a | b;  // 7
result = a ^ b;  // 6
result = ~a;  // -6
result = a << 1;  // 10
result = a >> 1;  // 2
    

Operator Precedence

Java follows a specific order of precedence for operators. When multiple operators are used in an expression, those with higher precedence are evaluated first.

Precedence Operator
1 Postfix (expr++, expr--)
2 Unary (++expr, --expr, +expr, -expr, ~, !)
3 Multiplicative (*, /, %)
4 Additive (+, -)
5 Shift (<<, >>, >>>)
6 Relational (<, >, <=, >=, instanceof)
7 Equality (==, !=)
8 Bitwise AND (&)
9 Bitwise XOR (^)
10 Bitwise OR (|)
11 Logical AND (&&)
12 Logical OR (||)
13 Ternary (?:)
14 Assignment (=, +=, -=, *=, /=, %=, &=, ^=, |=, <<=, >>=, >>>=)

Best Practices

  • Use parentheses to clarify the order of operations in complex expressions.
  • Be cautious when using the increment (++) and decrement (--) operators in expressions.
  • Avoid using the assignment operator (=) in conditional statements to prevent confusion with the equality operator (==).
  • When working with Java Booleans, use the logical operators (&&, ||) instead of bitwise operators (&, |) for better performance.

Understanding Java operators is crucial for writing efficient and effective Java code. They form the foundation for manipulating data and controlling program flow in Java Classes and Objects. As you progress in your Java journey, you'll find these operators essential in various aspects of programming, from simple calculations to complex Java Conditional Statements and Java Loops.