Java Operators: Compute Values with Confidence
Introduction
In this chapter, you will learn Java operators—symbols that perform calculations, comparisons, and logical combinations. Operators turn raw values into useful results for decisions and programs. They build directly on Variables and prepare you for conditionals.
Prerequisites
- JDK 21 and a working
mainclass - Basic understanding of Variables
- Ability to run code in IntelliJ or the terminal
What Is an Operator
An operator tells Java what action to perform on values (operands).
Simple metaphor:
- Values are ingredients
- Operators are actions (
+combine,*scale,==compare) - The expression’s result is the dish
int total = 3 + 5;
System.out.println(total); // 81) Arithmetic Operators
| Operator | Meaning | Example (int a = 10, b = 3) |
|---|---|---|
+ | Addition | a + b → 13 |
- | Subtraction | a - b → 7 |
* | Multiplication | a * b → 30 |
/ | Division | a / b → 3 (integer division) |
% | Remainder (modulo) | a % b → 1 |
public class ArithmeticDemo {
public static void main(String[] args) {
int a = 10;
int b = 3;
System.out.println(a + b); // 13
System.out.println(a - b); // 7
System.out.println(a * b); // 30
System.out.println(a / b); // 3 (not 3.333...)
System.out.println(a % b); // 1
}
}Tip
Practical Hint
Use n % 2 == 0 to test even numbers. Use % for wrap-around patterns such as clock arithmetic.
Integer vs Floating-Point Division
If either operand is double (or float), / performs floating-point division:
System.out.println(10 / 3); // 3
System.out.println(10.0 / 3); // 3.333...
System.out.println((double) 10 / 3); // 3.333...Exponent
Java has no ** operator. Use Math.pow(base, exponent) (returns double):
double power = Math.pow(2, 10); // 1024.02) Assignment Operators
= assigns a value. Compound operators update and assign in one step.
| Operator | Example | Same as |
|---|---|---|
= | x = 5 | assign |
+= | x += 2 | x = x + 2 |
-= | x -= 2 | x = x - 2 |
*= | x *= 2 | x = x * 2 |
/= | x /= 2 | x = x / 2 |
%= | x %= 2 | x = x % 2 |
int score = 50;
score += 10; // 60
score -= 5; // 55
score *= 2; // 110
System.out.println(score);3) Increment and Decrement (Unary)
++ and -- add or subtract 1. Used often in loops (later chapter).
int count = 0;
count++; // count is 1
count--; // count is 0 againPrefix (++i) vs postfix (i++) differ in expressions that use the value in the same statement. For now, using count += 1 is clearer until you are comfortable with loops.
4) Comparison Operators
Comparisons produce boolean results (true or false).
| Operator | Meaning |
|---|---|
== | equal |
!= | not equal |
> | greater than |
< | less than |
>= | greater than or equal |
<= | less than or equal |
int x = 8;
int y = 12;
System.out.println(x == y); // false
System.out.println(x != y); // true
System.out.println(x < y); // true
System.out.println(x >= y); // falseWarning
Do not confuse = and ==.
= assigns; == compares primitive values.
For String text, use .equals() instead of == in most cases—covered in Strings.
5) Logical Operators
Combine boolean conditions.
| Operator | Meaning |
|---|---|
&& | logical AND (both true) |
|| | logical OR (at least one true) |
! | logical NOT (invert) |
int age = 20;
boolean hasTicket = true;
boolean canEnter = age >= 18 && hasTicket;
System.out.println(canEnter); // trueJava uses short-circuit evaluation: in a && b, if a is false, b is not evaluated; in a || b, if a is true, b is not evaluated. This matters when the right side has side effects—advanced topic for now, but good to know the name.
6) Operator Precedence (Execution Order)
When multiple operators appear in one expression, Java follows precedence rules.
Simplified order (high to low):
- Parentheses
( ) - Unary
++,--,! - Multiplicative
*,/,% - Additive
+,- - Relational
<,<=,>,>= - Equality
==,!= - Logical
&& - Logical
|| - Assignment
=,+=, …
int result1 = 2 + 3 * 4; // 14 (multiply first)
int result2 = (2 + 3) * 4; // 20 (parentheses first)
System.out.println(result1);
System.out.println(result2);When in doubt, use parentheses to show intent. Clear code beats memorizing every precedence level.
Mini Practice
public class OperatorPractice {
public static void main(String[] args) {
int num1 = 15;
int num2 = 4;
System.out.println("sum: " + (num1 + num2));
System.out.println("difference: " + (num1 - num2));
System.out.println("product: " + (num1 * num2));
System.out.println("division: " + (num1 / num2));
System.out.println("remainder: " + (num1 % num2));
System.out.println("num1 > num2: " + (num1 > num2));
System.out.println("both positive: " + (num1 > 0 && num2 > 0));
}
}Common Beginner Mistakes
Mistake 1: Integer Division Surprise
10 / 3 is 3, not 3.33. Cast or use double when you need decimals.
Mistake 2: Forgetting Parentheses
2 + 3 * 4 is not 20. Parentheses document your intent.
Mistake 3: Using = in a Condition
// if (age = 18) { } // Compile error in Java — good!
if (age == 18) { }Java prevents many accidental assignments in conditions; still be deliberate with ==.
What’s Next
Deepen numeric types in Numbers, then build your first interactive program in Sum of Two Numbers.
FAQ
Which operators should I memorize first?
Arithmetic (+ - * / %), comparison (== != > <), and logical (&& || !). That covers most beginner tasks.
Why is 10 / 4 equal to 2?
Both operands are int, so Java performs integer division and truncates toward zero.
When should I use %?
When you need remainders: even/odd checks, cycling indexes, or “every Nth item” logic.
Does Java have and / or keywords like Python?
No. Java uses && and || for boolean logic.
Is operator precedence important for beginners?
Yes. Use parentheses until precedence feels natural.