πŸ’©

programierds

Back to course
Lessons 4 / 12

Operators in Java

Operators

Operators are symbols that tell the program it must perform a specific operation on data.

Arithmetic Operators

They are used for mathematical calculations:

  • + (Addition): 5 + 3 = 8
  • - (Subtraction): 7 - 4 = 3
  • * (Multiplication): 3 * 4 = 12
  • / (Division): 12 / 4 = 3
  • % (Modulo - division remainder): 13 % 4 = 1
  • ++ (Increment): i++ increases by 1.
  • -- (Decrement): j-- decreases by 1.

Comparison Operators

They return a boolean value (true or false):

  • == (Equality): a == b
  • != (Inequality): a != b
  • > (Greater than): a > b
  • < (Less than): a < b
  • >= (Greater than or equal): a >= b
  • <= (Less than or equal): a <= b

Logical Operators

They are used to combine boolean expressions:

  • && (AND): True if both are true.
  • || (OR): True if at least one is true.
  • ! (NOT): Negates the value (changes true to false and vice versa).

Assignment Operators

  • = (Simple assignment): x = 10
  • +=: x += 3 is the same as x = x + 3
  • -=: x -= 4
  • *=: x *= 5
  • /=: x /= 2
  • %=: x %= 3