Java Operators - datacouch-io/spark-java GitHub Wiki

In Java, operators are essential tools for performing various operations on variables and values. Each operation involves two operands and is defined by an operator.

Operators in Java are grouped into the following categories:

Arithmetic Operators

Arithmetic operators are used for common mathematical operations:

  • + (Addition): Adds two values together (e.g., x + y).
  • - (Subtraction): Subtracts one value from another (e.g., x - y).
  • * (Multiplication): Multiplies two values (e.g., x * y).
  • / (Division): Divides one value by another (e.g., x / y).
  • % (Modulus): Returns the remainder of a division (e.g., x % y).
  • ++ (Increment): Increases the value of a variable by 1 (e.g., ++x).
  • -- (Decrement): Decreases the value of a variable by 1 (e.g., --x).

Note: ++x is called pre-increment (increments the value of x and then returns x), while x++ is called post-increment (returns the value of x and then increments it).

Examples:

int x = 5, y = 5;

System.out.println(++x); // Outputs 6
System.out.println(x);   // Outputs 6

System.out.println(y++); // Outputs 5
System.out.println(y);   // Outputs 6

Assignment Operators

Assignment operators are used to assign values to variables and perform operations simultaneously:

  • = (Assignment): Assigns a value to a variable (e.g., x = 10).
  • += (Addition Assignment): Adds a value to a variable (e.g., x += 5 is equivalent to x = x + 5).
  • -= (Subtraction Assignment): Subtracts a value from a variable (e.g., x -= 3 is equivalent to x = x - 3).
  • *= (Multiplication Assignment): Multiplies a variable by a value (e.g., x *= 3 is equivalent to x = x * 3).
  • /= (Division Assignment): Divides a variable by a value (e.g., x /= 3 is equivalent to x = x / 3).
  • %= (Modulus Assignment): Calculates the modulus and assigns the result to a variable (e.g., x %= 3 is equivalent to x = x % 3).
  • &= (Bitwise AND Assignment): Performs a bitwise AND operation and assigns the result to a variable.
  • |= (Bitwise OR Assignment): Performs a bitwise OR operation and assigns the result to a variable.
  • ^= (Bitwise XOR Assignment): Performs a bitwise XOR operation and assigns the result to a variable.
  • >>= (Right Shift Assignment): Right-shifts a variable and assigns the result (e.g., x >>= a is equivalent to x = x / 2^a).
  • <<= (Left Shift Assignment): Left-shifts a variable and assigns the result (e.g., x <<= a is equivalent to x = x * 2^a).

Examples:

int x = 5;
x &= 3;
System.out.println(x); // Outputs 1
int x = 5;
x |= 3; // Equivalent to x = x | 3
System.out.println(x); // Outputs 7

Comparison Operators

Comparison operators are used to compare two values:

  • == (Equal to): Returns true if two values are equal (e.g., x == y).
  • != (Not equal): Returns true if two values are not equal (e.g., x != y).
  • > (Greater than): Returns true if the left operand is greater than the right operand (e.g., x > y).
  • < (Less than): Returns true if the left operand is less than the right operand (e.g., x < y).
  • >= (Greater than or equal to): Returns true if the left operand is greater than or equal to the right operand (e.g., x >= y).
  • <= (Less than or equal to): Returns true if the left operand is less than or equal to the right operand (e.g., x <= y).

Logical Operators

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

  • && (Logical AND): Returns true if both statements are true (e.g., A && B).
  • || (Logical OR): Returns true if at least one of the statements is true (e.g., A || B).
  • ! (Logical NOT): Reverses the result, returns false if the result is true (e.g., !A).

These operators are fundamental in Java and play a crucial role in performing various operations and making decisions in your programs.