Operators in Java

Operators in Java are symbols or special keywords used to perform various operations on variables, values, or expressions. Java supports a wide range of operators, which can be categorized into different types:

Arithmetic Operators

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

Assignment Operators

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

Increment and Decrement Operators

  • Increment (++): Increases the value of a variable by 1.
  • Decrement (–): Decreases the value of a variable by 1.

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 operand.
  • Less than (<): Checks if the left operand is less than the right operand.
  • Greater than or equal to (>=): Checks if the left operand is greater than or equal to the right operand.
  • Less than or equal to (<=): Checks if the left operand is less than or equal to the right operand.

Logical Operators

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

Bitwise Operators

  • Bitwise AND (&): Performs a bitwise AND operation between two operands.
  • Bitwise OR (|): Performs a bitwise OR operation between two operands.
  • Bitwise XOR (^): Performs a bitwise exclusive OR operation between two operands.
  • Bitwise NOT or Complement (~): Inverts the bits of the operand.
  • Left Shift (<<): Shifts the bits of the left operand to the left by a specified number of positions.
  • Right Shift (>>): Shifts the bits of the left operand to the right by a specified number of positions.
  • Unsigned Right Shift (>>>): Shifts the bits of the left operand to the right by a specified number of positions, filling the leftmost bits with zeros.

Conditional (Ternary) Operator

Ternary Operator (condition ? expr1 : expr2): Evaluates the condition and returns expr1 if true, otherwise returns expr2.

Frequently Asked Questions

What are operators in Java?

Operators in Java are special symbols or keywords used to perform operations on variables and values, such as addition, subtraction, and comparison.

How are operators classified in Java?

Operators in Java are classified into categories, including arithmetic, assignment, relational, logical, bitwise, and conditional (ternary) operators.

What is operator precedence in Java?

Operator precedence determines the order in which operators are evaluated in an expression. Operators with higher precedence are evaluated first.

What is the difference between the == operator and the equals() method for comparing objects in Java?

The == operator compares object references, checking if two references point to the same object. The equals() method is used to compare the contents or values of objects.

What are the short-circuit logical operators (&& and ||) in Java?

The short-circuit logical operators in Java (&& and ||) evaluate only as many sub-expressions as necessary to determine the final result, potentially improving performance and avoiding unnecessary evaluation.

Sharing Is Caring:
Subscribe
Notify of
0 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments