What are operators in Java and how are they categorized?

In programming, we frequently need to perform operations like arithmetic, comparisons, or logical evaluations. Without operators, we wouldn't be able to manipulate data or make decisions within a program.

🔍 What is it?

Operators in Java are special symbols or keywords that perform specific operations on operands (variables or values). They enable arithmetic operations, comparisons, logic evaluations, and more.

How are operators categorized in Java?

  1. Arithmetic Operators

Used to perform basic mathematical operations.

List of Arithmetic Operators:

- + (Addition): Adds two operands.

- - (Subtraction): Subtracts the right operand from the left.

- * (Multiplication): Multiplies two operands.

- / (Division): Divides the left operand by the right (produces a quotient).

- % (Modulus): Divides the left operand by the right and returns the remainder.

Example:

java int a = 10, b = 5; int sum = a + b; // sum = 15 int remainder = a % b; // remainder = 0

  1. Relational (Comparison) Operators

Used to compare two values and return a boolean result (true or false).

List of Relational Operators:

- == (Equal to): Checks if two operands are equal.

- != (Not equal to): Checks if two operands are not equal.

- > (Greater than): Checks if the left operand is greater than the right.

- < (Less than): Checks if the left operand is less than the right.

- >= (Greater than or equal to): Checks if the left operand is greater than or equal to the right.

- <= (Less than or equal to): Checks if the left operand is less than or equal to the right.

Example:

java int x = 10, y = 20; boolean result = x < y; // result = true

  1. Logical Operators

Used to combine two or more boolean expressions or values and return a boolean result.

List of Logical Operators:

- && (Logical AND): Returns true if both operands are true.

- || (Logical OR): Returns true if at least one operand is true.

- ! (Logical NOT): Reverses the logical state of its operand.

Example:

java boolean a = true, b = false; boolean result = a && b; // result = false (both need to be true)

  1. Assignment Operators

Used to assign values to variables.

List of Assignment Operators:

- = (Assignment): Assigns the right-hand value to the left-hand variable.

- +=, -=, *=, /=, %=: These are shorthand operators that perform the operation and then assign the result to the left operand.

Example:

java int a = 10; a += 5; // a = 15

  1. Unary Operators

Operate on a single operand and perform operations like incrementing, decrementing, negating, or inverting the value.

List of Unary Operators:

- ++ (Increment): Increases the value by 1.

- -- (Decrement): Decreases the value by 1.

- + (Unary plus): Positive value (default).

- - (Unary minus): Negative value.

- ! (Logical NOT): Inverts the value of a boolean.

Example:

java int a = 5; a++; // a = 6

  1. Bitwise Operators

Perform operations at the bit level. These operators work on integers and perform bitwise logic.

List of Bitwise Operators:

- & (Bitwise AND): Performs AND operation on each bit of two operands.

- | (Bitwise OR): Performs OR operation on each bit of two operands.

- ^ (Bitwise XOR): Performs XOR operation on each bit of two operands.

- ~ (Bitwise NOT): Inverts each bit of the operand.

- << (Left shift): Shifts bits to the left, filling with zeros.

- >> (Right shift): Shifts bits to the right, preserving the sign bit.

- >>> (Unsigned right shift): Shifts bits to the right, filling with zeros.

Example:

java int a = 5, b = 3; int result = a & b; // result = 1 (binary AND)

  1. Ternary Operator (? :)

The ternary operator is a shorthand for an if-else statement and operates on three operands.

Syntax:

java condition ? value_if_true : value_if_false;

Example:

java int x = 10, y = 20; int result = (x < y) ? x : y; // result = 10

  1. Instanceof Operator

Used to test whether an object is an instance of a specific class or subclass.

Example:

java String str = "Hello"; boolean result = str instanceof String; // result = true

Summary of Operator Categories:

  • Arithmetic operators perform mathematical operations.
  • Relational operators compare values and return boolean results.
  • Logical operators work with boolean values to perform logical operations.
  • Assignment operators assign values to variables.
  • Unary operators work with single operands to perform operations like increment, decrement, or negation.
  • Bitwise operators perform operations at the bit level.
  • Ternary operator acts as a concise if-else statement.
  • Instanceof checks if an object belongs to a particular class.

In conclusion, operators in Java are essential for performing computations, comparisons, and logical operations. They are categorized into different types based on their functionality, making Java a powerful language for handling a wide range of operations.