Skip to main content

Operators

Now that you know how to store data in variables, you need to know how to work with that data. In Java, we use operators to perform calculations, make comparisons, and combine values.

What is an Operator?

An operator is a symbol that tells Java to perform a specific operation on one or more values. Most operators work with two values (like 5 + 3), but some work with just one.

Example:

int result = 10 + 5; // The + operator adds 10 and 5

Arithmetic Operators

These are the math operators you already know, plus a few extras.

Basic Math

int a = 10;
int b = 3;

int sum = a + b; // Addition: 13
int difference = a - b; // Subtraction: 7
int product = a * b; // Multiplication: 30
int quotient = a / b; // Division: 3 (integer division!)
int remainder = a % b; // Modulo (remainder): 1
tip

When dividing integers, Java throws away the decimal part. So 10 / 3 equals 3, not 3.33. Use double if you need decimals!

Special Assignment Operators

These let you modify a variable and store the result back in the same variable:

int score = 100;

score += 10; // Same as: score = score + 10; (now 110)
score -= 5; // Same as: score = score - 5; (now 105)
score *= 2; // Same as: score = score * 2; (now 210)
score /= 3; // Same as: score = score / 3; (now 70)

Increment and Decrement

For adding or subtracting 1, there's an even shorter way:

int lives = 3;

lives++; // Same as: lives = lives + 1; (now 4)
lives--; // Same as: lives = lives - 1; (now 3)

Comparison Operators

These operators compare two values and give you back a boolean (true or false).

int x = 5;
int y = 10;

boolean isEqual = (x == y); // Equal to: false
boolean isNotEqual = (x != y); // Not equal to: true
boolean isLess = (x < y); // Less than: true
boolean isGreater = (x > y); // Greater than: false
boolean isLessOrEqual = (x <= y); // Less than or equal: true
boolean isGreaterOrEqual = (x >= y); // Greater than or equal: false
tip

Notice the double equals == for comparison vs. single equals = for assignment.

Logical Operators

These work with boolean values to create more complex conditions.

boolean isRunning = true;
boolean hasAmmo = false;

boolean canShoot = isRunning && hasAmmo; // AND: both must be true (false)
boolean canMove = isRunning || hasAmmo; // OR: at least one must be true (true)
boolean isStopped = !isRunning; // NOT: flips the value (false)

String Concatenation

The + operator has a special power with String variables — it joins them together:

String firstName = "John";
String lastName = "Doe";
int age = 16;

String fullName = firstName + " " + lastName; // "John Doe"
String message = "Hello, " + firstName + "!"; // "Hello, John!"
String info = firstName + " is " + age; // "John is 16"

Order of Operations

Just like in math, Java follows a specific order when evaluating expressions:

  1. Parentheses ()
  2. Multiplication, Division, Modulo *, /, %
  3. Addition, Subtraction +, -
  4. Comparison <, >, <=, >=
  5. Equality ==, !=
  6. Logical AND &&
  7. Logical OR ||
int result = 2 + 3 * 4;        // 14, not 20
int result2 = (2 + 3) * 4; // 20

Quick Reference

CategoryOperatorWhat It DoesExample
Arithmetic+Addition5 + 38
-Subtraction5 - 32
*Multiplication5 * 315
/Division5 / 31
%Remainder5 % 32
Assignment+=Add and assignx += 5
++Increment by 1x++
--Decrement by 1x--
Comparison==Equal tox == 5
!=Not equal tox != 5
<Less thanx < 5
>Greater thanx > 5
Logical&&ANDtrue && falsefalse
││ORtrue ││ falsetrue
!NOT!truefalse