Java Operators – A Detailed Blog

There are eight types of operators in Java. Let’s see about each and every operator with example,

1. Arithmetic Operators

Arithmetic operators perform mathematical operations on operands.

  • + (Addition): Adds two operands.
  • - (Subtraction): Subtracts the second operand from the first.
  • * (Multiplication): Multiplies two operands.
  • / (Division): Divides the first operand by the second.
  • % (Modulus): Returns the remainder of division.

Example:

int a = 10;
int 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.

  • = (Assignment): Assigns the value on the right to the variable on the left.
  • Compound Assignment Operators: Combine an arithmetic operator with assignment (e.g., +=, -=, *=, /=, %=).

Example:

int x = 10;
x += 5;   // x = x + 5; (x is now 15)
x -= 3;   // x = x - 3; (x is now 12)
x *= 2;   // x = x * 2; (x is now 24)
x /= 4;   // x = x / 4; (x is now 6)
x %= 5;   // x = x % 5; (x is now 1)

3. Unary Operators

Unary operators operate on a single operand.

  • + (Unary Plus): Indicates a positive value.
  • - (Unary Minus): Negates the value.
  • ++ (Increment): Increases the value by 1.
  • -- (Decrement): Decreases the value by 1.
  • ! (Logical Complement): Reverses the logical state of the operand.

Example:

int x = 10;
int y = -x;     // -10
int a = 5;
int b = ++a;    // a = 6, b = 6 (pre-increment)
int c = 5;
int d = c++;    // c = 6, d = 5 (post-increment)
boolean flag = true;
boolean result = !flag;  // false

4. Relational Operators

Relational operators compare two values.

  • == (Equal to): Checks if two operands are equal.
  • != (Not equal to): Checks if two operands are not equal.
  • > (Greater than), < (Less than), >= (Greater than or equal to), <= (Less than or equal to): Compare two operands.

Example:

int a = 10;
int b = 5;
boolean isEqual = (a == b);      // false
boolean isNotEqual = (a != b);   // true
boolean isGreater = (a > b);     // true
boolean isLess = (a < b);        // false
boolean isGreaterOrEqual = (a >= b); // true
boolean isLessOrEqual = (a <= b);    // false

5. Logical Operators

Logical operators perform logical operations on boolean operands.

  • && (Logical AND): Returns true if both operands are true.
  • || (Logical OR): Returns true if at least one operand is true.
  • ! (Logical NOT): Inverts the logical state of its operand.

Example:

boolean x = true;
boolean y = false;
boolean result1 = x && y;  // false
boolean result2 = x || y;  // true
boolean result3 = !x;      // false

6. Bitwise Operators

Bitwise operators perform bitwise operations on integer operands.

  • & (Bitwise AND), | (Bitwise OR), ^ (Bitwise XOR): Perform bitwise operations on operands.
  • ~ (Bitwise Complement): Inverts all the bits of its operand.
  • << (Left Shift), >> (Right Shift), >>> (Unsigned Right Shift): Shifts the bits of the first operand left or right by the number of positions specified by the second operand.

Example:

int a = 5;    // 0000 0101
int b = 3;    // 0000 0011
int bitwiseAnd = a & b;   // 0000 0001 (1)
int bitwiseOr = a | b;    // 0000 0111 (7)
int bitwiseXor = a ^ b;   // 0000 0110 (6)
int bitwiseComplement = ~a;  // 1111 1010 (-6)
int leftShift = a << 1;    // 0000 1010 (10)
int rightShift = a >> 1;   // 0000 0010 (2)

7. Conditional Operator (Ternary Operator)

The conditional operator is a ternary operator used for conditional expressions.

  • ? : (Ternary Operator): Provides a compact way to write conditional statements.

Example:

int x = 10;
int y = (x > 5) ? 1 : -1;  // y = 1

8. instanceof Operator

The instanceof operator checks whether an object is an instance of a particular class or interface.

  • instanceof: Checks if an object is an instance of a particular class or interface.

Example:

String str = "Hello";
boolean result = (str instanceof String);  // true

9. Precedence and Associativity

  • Operators have precedence and associativity rules that determine the order of evaluation in expressions.

How useful was this post?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.