12. Types of Operator part2

 

6. Conditional Operator

Also known as the ternary operator, it returns one of two values based on a condition.

  • Ternary (?:): If condition is true, return value1; else, return value2.
    int max = (a > b) ? a : b; // max is 5

7. Compound Assignment Operator

These combine an arithmetic operation with assignment.

  • Add and assign (+=): Adds right operand to the left operand and assigns the result to the left operand.

    a += b; // a becomes 8 (5 + 3)
  • Subtract and assign (-=): Subtracts right operand from the left operand and assigns the result to the left operand.

    a -= b; // a becomes 2 (5 - 3)

8. Increment and Decrement Operator

These are used to increase or decrease the value of a variable by 1.

  • Increment (++): Increases value by 1.

    a++; // a becomes 6
  • Decrement (--): Decreases value by 1.

    a--; // a becomes 4

9. Miscellaneous Operator

Other useful operators.

  • Instanceof: Checks if an object is an instance of a specific class or subclass.

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

Comments