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
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
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:
- Parentheses
() - Multiplication, Division, Modulo
*,/,% - Addition, Subtraction
+,- - Comparison
<,>,<=,>= - Equality
==,!= - Logical AND
&& - Logical OR
||
int result = 2 + 3 * 4; // 14, not 20
int result2 = (2 + 3) * 4; // 20
Quick Reference
| Category | Operator | What It Does | Example |
|---|---|---|---|
| Arithmetic | + | Addition | 5 + 3 → 8 |
- | Subtraction | 5 - 3 → 2 | |
* | Multiplication | 5 * 3 → 15 | |
/ | Division | 5 / 3 → 1 | |
% | Remainder | 5 % 3 → 2 | |
| Assignment | += | Add and assign | x += 5 |
++ | Increment by 1 | x++ | |
-- | Decrement by 1 | x-- | |
| Comparison | == | Equal to | x == 5 |
!= | Not equal to | x != 5 | |
< | Less than | x < 5 | |
> | Greater than | x > 5 | |
| Logical | && | AND | true && false → false |
││ | OR | true ││ false → true | |
! | NOT | !true → false |