Lesson 12 +10 XP

Java Operators

Java Operators

Operators are symbols that perform operations on values. Java groups them into several categories.

Arithmetic operators

Used for math:

OperatorNameExample
+Additionx + y
-Subtractionx - y
*Multiplicationx * y
/Divisionx / y
%Modulus (remainder)x % y
++Increment++x
--Decrement--x
int sum1 = 100 + 50;
int sum2 = sum1 + 250;
System.out.println(sum2); // 400

Assignment operators

The = operator assigns a value. It combines with arithmetic in shortcuts:

int x = 10;
x += 5;  // same as x = x + 5
x -= 3;  // same as x = x - 3
x *= 2;  // same as x = x * 2
x /= 4;  // same as x = x / 4
x %= 3;  // same as x = x % 3

Comparison operators

Compare two values and return true or false:

  • == equal to
  • != not equal
  • > greater than
  • < less than
  • >= greater than or equal
  • <= less than or equal
int x = 5;
System.out.println(x == 3); // false
System.out.println(x != 3); // true

Logical operators

Combine conditions:

OperatorNameExample
&&Logical andx < 5 && y > 1
``Logical or`x < 5y > 1`
!Logical not!(x < 5)

TL;DR

  • Arithmetic: + - * / % ++ --.
  • Assignment shortcuts: += -= *= /= %=.
  • Comparison returns true or false.
  • &&, ||, and ! combine conditions.