Operators

Java operators are symbols that are used to perform operations on variables and values. These operations can be mathematical, relational, or logical. Here are some of the most commonly used operators in Java:

  1. Arithmetic Operators:

    • Addition +: Adds two operands.

    • Subtraction -: Subtracts the right operand from the left operand.

    • Multiplication *: Multiplies two operands.

    • Division /: Divides the left operand by the right operand.

    • Modulus %: Returns the remainder after division.

    Example:

    int a = 10, b = 3;
    int sum = a + b; // sum = 13
    int difference = a - b; // difference = 7
    int product = a * b; // product = 30
    int quotient = a / b; // quotient = 3
    int remainder = a % b; // remainder = 1
  2. 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.

    Example:

    int x = 5, y = 10;
    boolean isEqual = x == y; // isEqual = false
    boolean isNotEqual = x != y; // isNotEqual = true
    boolean isGreaterThan = x > y; // isGreaterThan = false
    boolean isLessThan = x < y; // isLessThan = true
    boolean isGreaterOrEqual = x >= y; // isGreaterOrEqual = false
    boolean isLessOrEqual = x <= y; // isLessOrEqual = true
  3. 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.

    Example:

    boolean isJavaFun = true;
    boolean isProgrammingHard = false;
    boolean bothTrue = isJavaFun && isProgrammingHard; // bothTrue = false
    boolean eitherTrue = isJavaFun || isProgrammingHard; // eitherTrue = true
    boolean notTrue = !isJavaFun; // notTrue = false
  4. Assignment Operators:

    • Assignment =: Assigns the value on the right to the variable on the left.

    • Compound Assignment operators (e.g., +=, -=, *=, /=, %=): Perform an operation and assign the result back to the variable.

    Example:

    int num = 5;
    num += 3; // num is now 8 (equivalent to num = num + 3)
  5. Increment and Decrement Operators:

    • Increment ++: Increases the value of a variable by 1.

    • Decrement --: Decreases the value of a variable by 1.

    Example:

    int count = 10;
    count++; // count is now 11
    count--; // count is now 10
  6. Bitwise Operators (for integer types):

    • Bitwise AND &

    • Bitwise OR |

    • Bitwise XOR ^

    • Bitwise NOT ~

    • Left Shift <<

    • Right Shift >>

    • Unsigned Right Shift >>>

    Example:

    int a = 5; // Binary: 0101
    int b = 3; // Binary: 0011
    int resultAnd = a & b; // resultAnd = 1 (Binary: 0001)
  7. Conditional (Ternary) Operator:

    The conditional operator ? : is a shorthand way of writing an if-else statement.

    Example:

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

These are the most commonly used operators in Java. Understanding how to use these operators is essential for writing Java programs.

Last updated