Muhammad Fari Madyan
Hire me
Java basics · part 5 of 21

Learn Java from Scratch Part 5: Operators — Arithmetic, Comparison, Logic & the Mystery of 7 / 2

The fifth part of a Java series for complete beginners: arithmetic operators and modulo, why 7 / 2 is 3, comparison operators as a pair of scales, the AND, OR and NOT gates, a shopping example with a discount, and three traps such as dividing by zero.

Muhammad Fari Madyan

· 8 min read

Baca dalam Bahasa Indonesia →

Introduction: Where Did the Half Go?

Seven divided by two. Three and a half, right?

Ask Java, and the answer is three.

Where did the half go? In this part we meet operators. They are the tools Java uses to calculate and to compare.

After this part, you will know:

  1. How to calculate with arithmetic operators, including modulo.
  2. How to compare two values, and why the result is always true or false.
  3. How to combine several conditions with AND, OR and NOT.

Answer to the Part 4 Challenge

The question: println("Skor: " + 2 + 3). What is the output?

The answer is B: Skor: 23.

Java reads from the left. "Skor: " is a string, so 2 is joined as text. The result is still a string, so 3 is joined too.

If you want the numbers to be added first, use brackets:

JAVA
IO.println("Skor: " + (2 + 3));
TEXT
Skor: 5

If you answered B, well done!


1. The Problem: A Program Must Calculate and Decide

A useful program almost always calculates and decides.

  • What is the total price after the discount?
  • Is the exam score high enough to pass?
  • Is the user logged in and allowed to enter?

For all of this, Java gives us operators: symbols that work on the values in our boxes. We learn three groups here.


2. Group One: Arithmetic Operators

Create a file named Hitung.java. Hitung means "calculate".

JAVA
void main() {
    IO.println(7 + 2);
    IO.println(7 - 2);
    IO.println(7 * 2);
    IO.println(7 / 2);
    IO.println(7 % 2);
}
TEXT
9
5
14
3
1
OperatorMeaningExampleResult
+Add7 + 29
-Subtract7 - 25
*Multiply7 * 214
/Divide7 / 23
%Modulo, which is the remainder of a division7 % 21

The one that few people know is %. Seven divided by two is three, with one left over. That leftover is the result of modulo.

Modulo is useful to check if a number is even or odd. If the remainder after dividing by two is 0, the number is even.

As in maths, multiplication and division are done first. If you want the addition first, use brackets.

CodeResult
2 + 3 * 414
(2 + 3) * 420

3. The Mystery of Seven Divided by Two

Now back to the mystery from the introduction. Why is 7 / 2 equal to 3?

Remember the box rule from Part 3. 7 and 2 are both int. So the result also goes into an int box.

An int box has no room for digits after the decimal point. So the half is thrown away. It is not rounded.

If one of the two numbers is a double, the result goes into a double box:

CodeType of the resultResult
7 / 2int3
7 / 2.0double3.5

4. Group Two: Comparison Operators

Picture a pair of scales. You put one value on the left and one value on the right, and you ask: which one is heavier?

OperatorMeaning
>Greater than
<Less than
>=Greater than or equal to
<=Less than or equal to
==Equal to
!=Not equal to

Look at ==. To check if two values are equal, use two equals signs.

The result of a comparison is always a boolean: true or false.

JAVA
void main() {
    int nilai = 80;
    boolean lulus = nilai >= 75;
    IO.println(lulus);
}
TEXT
true

Here nilai is the exam score and lulus means "passed". The score is 80 and the limit is 75. The scales tip, and the lulus box holds true.


5. Group Three: Logical Operators

Logical operators combine several conditions. Picture two kinds of gates.

The AND gate, written &&, has two locks. It opens only when both conditions are true.

The OR gate, written ||, has two doors. One true condition is enough.

Then there is the exclamation mark !, which is called NOT. It flips a value: true becomes false, and false becomes true.

Scales for comparison, an AND gate with two locks, and an OR gate with two doors

Click the diagram to open it in full size.

JAVA
void main() {
    boolean lulus = true;
    boolean hadir = false;

    IO.println(lulus && hadir);
    IO.println(lulus || hadir);
    IO.println(!lulus);
}
TEXT
false
true
false

The variable hadir means "present in class".

CodeRead asResult
lulus && hadirpassed and presentfalse, because hadir is false
lulus || hadirpassed or presenttrue, because lulus is true
!lulusnot passedfalse

A bonus: two short forms that you will see very often.

JAVA
void main() {
    int skor = 10;
    skor += 5;
    IO.println(skor);
    skor++;
    IO.println(skor);
}
TEXT
15
16

skor += 5 means: add five to skor. skor++ means: add one to skor.


6. A Real Example: Shopping with a Discount

An item costs 85,000 rupiah and the discount is 20 percent. Shipping is free when the payment is at least 50,000 rupiah.

Create a file named Belanja.java, which means "shopping":

JAVA
void main() {
    int harga = 85_000;
    int diskon = harga * 20 / 100;
    int bayar = harga - diskon;
    boolean gratisOngkir = bayar >= 50_000;

    IO.println("Diskon: " + diskon);
    IO.println("Bayar: " + bayar);
    IO.println("Gratis ongkir: " + gratisOngkir);
}
TEXT
Diskon: 17000
Bayar: 68000
Gratis ongkir: true

The names mean: harga is the price, diskon is the discount, bayar is the amount to pay, and gratisOngkir means "free shipping".

Three groups of operators work together here: arithmetic to calculate, comparison to decide, and the + from Part 4 to join text.

Now look at the order in the diskon line. If 20 / 100 is calculated first, the result is 0, because int division throws away the decimals.

CodeResult
harga * 20 / 10017000
harga * (20 / 100)0

The discount becomes zero rupiah. So multiply first, then divide.


7. Three Operator Traps

Trap one: int division throws away the decimals.

If you need a decimal result, make sure one of the numbers is a double. Write 7 / 2.0, not 7 / 2.

Trap two: one equals sign for a comparison.

One = puts a value into a box. Two == compare two values. If you mix them up, javac complains:

JAVA
void main() {
    int nilai = 80;
    boolean sempurna = nilai = 100;
    IO.println(sempurna);
}
TEXT
Hitung.java:3: error: incompatible types: int cannot be converted to boolean
    boolean sempurna = nilai = 100;
                             ^
1 error

The correct line is boolean sempurna = nilai == 100;. The name sempurna means "perfect".

Trap three: dividing by zero.

JAVA
void main() {
    int kue = 10;
    int orang = 0;
    IO.println(kue / orang);
}

This one is different. javac does not catch it, so the code compiles. But when the program runs, it stops at once:

TEXT
Exception in thread "main" java.lang.ArithmeticException: / by zero
	at Hitung.main(Hitung.java:4)

The message still gives you the address: the file Hitung.java, line 4. A mistake that only appears while the program runs, like this one, is called an exception. We cover exceptions in Part 18.


Summary

  1. Arithmetic operators calculate, including modulo for the remainder. int division throws away the decimals.
  2. Comparison operators work like scales, and the result is always true or false. To check if two values are equal, use ==.
  3. AND needs both conditions, OR needs only one, and NOT flips the value.

Challenge

Look at this code:

JAVA
void main() {
    IO.println(10 / 4 * 2);
}

What is the output?

  • A. 5
  • B. 4
  • C. 5.0

Try to answer before you run it. I explain the answer at the start of Part 6.


Conclusion & What's Next

Your program can now calculate, compare, and combine conditions. If you remember only one thing, remember int division: the digits after the decimal point are thrown away, not rounded.

All our programs so far are like a speech: they talk and never listen. In Part 6, our program starts to have a conversation. We add a funnel, so the user can type an answer and the program can read it.

Before you move on, change the price and the discount in Belanja.java, and guess the result before you run it.

See you in Part 6. Get your coffee ready. Hot chocolate is fine too.

ShareLinkedInXWhatsApp

Written by Muhammad Fari Madyan

Senior Backend Engineer and co-founder of LampungDev. I write up what I build so others can learn from it.

Work with me
← part 4Stringspart 6 →Reading User Input

Muhammad Fari Madyan

Senior Backend Engineer building real-time, high-concurrency systems that hold up in production. Co-founder of LampungDev. Based in Bandar Lampung, Indonesia (UTC+7), working remote.

Work

Case studiesCahTixExam platform, 4 stacksOpen source

© 2026 Muhammad Fari Madyan

Designed and built by Muhammad Fari Madyan