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

Learn Java from Scratch Part 3: Variables and Primitive Types — int, double, boolean, char & Boxes with Labels

The third part of a Java series for complete beginners: variables as boxes with labels, the four types you use most, what int and double look like in memory, why javac rejects a value that does not fit, and three traps such as writing decimals with a comma.

Muhammad Fari Madyan

· 8 min read

Baca dalam Bahasa Indonesia →

Introduction: Four Pieces of Data, One Rejection

Age: 17. Height: 165.5. Likes coffee? Yes. Initial: R.

Four simple pieces of data. But try to put the number 9.99 into the wrong box, and Java rejects it at once.

Why is Java so strict? In this part we find out.

After this part, you will know:

  1. What a variable is and how to create one.
  2. The four data types you will use most: int, double, boolean and char.
  3. Why javac rejects a value that does not fit its box.

Answer to the Part 2 Challenge

The question: print("Halo, "), then println("Dunia!"), then println("Siap belajar."). What is the output?

The answer is A.

TEXT
Halo, Dunia!
Siap belajar.

print does not move to a new line. So Dunia! continues on the same line. Only after println does the next text move down to the second line.

If you answered A, well done!


1. The Problem: A Program Must Remember

Our program in Part 2 could only show text that was already fixed.

A real program must remember things:

  • a game score that keeps growing,
  • a balance that goes down every time you shop,
  • a user name that someone has just typed.

All of these need a place to be stored. That place is called a variable.


2. A Variable Is a Box with a Label

Picture a variable as a box with a label.

  • The label is the name of the variable, for example umur. It means "age" in Indonesian.
  • The content is the value, for example 17.

In Java, every box also has a kind. This kind is called a data type.

A box for whole numbers only accepts whole numbers. A box for letters only accepts letters.

Before your program runs, javac checks the content of every box, like a guard at a door. A value that does not fit is rejected.

Strict? Yes. But because of this, many mistakes are found early, before anyone uses the program.

Four variable boxes: int, double, boolean and char, each with its label and value

Click the diagram to open it in full size.


3. The Four Types You Will Use Most

TypeUsed forExample value
intWhole numbers17, a number of items
doubleDecimal numbers165.5, a price, a height
booleanOnly two possible valuestrue or false
charOne character, between single quotes'R'

All four are primitive types. These are the most basic types in Java.

There are eight primitive types in total, because numbers have boxes of different sizes. Here are three of them:

TypeSizeHolds up to
byte1 byte-128 to 127
int4 bytesabout 2.1 billion
long8 bytesabout 9 quintillion

A bigger box uses more memory. A long box is big enough to count all the people on Earth many times over.

For learning, int and double are enough.


4. Writing the Code and Looking into Memory

Create a file named Kotak.java. Kotak means "box". Then type:

JAVA
void main() {
    int umur = 17;
    double tinggi = 165.5;
    boolean sukaKopi = true;
    char inisial = 'R';

    IO.println(umur);
    IO.println(tinggi);
    IO.println(sukaKopi);
    IO.println(inisial);
}

The names mean: umur is age, tinggi is height, sukaKopi is "likes coffee", and inisial is initial.

Run it with java Kotak.java. The result:

TEXT
17
165.5
true
R

Every line has the same pattern: type, name, the = sign, value, then a semicolon.

Now we look into memory. Picture the computer's memory as a row of small lockers. One locker stores one byte.

When Java runs int umur = 17;, three things happen:

  1. Java prepares an int box with the label umur.
  2. Java puts 17 into it.
  3. The box is placed in memory. An int box uses 4 bytes, so 4 lockers.

A double box is bigger. tinggi uses 8 bytes, so 8 lockers.

A look into memory: the int box umur uses 4 lockers, the double box tinggi uses 8 lockers

Click the diagram to open it in full size.


5. Changing the Content of a Box

The = sign here does not mean "equals" as in a maths lesson. It means: put the value on the right into the box on the left.

Add these two lines at the end of main:

JAVA
    umur = 18;
    IO.println(umur);

The result:

TEXT
18

There are three things to notice.

You do not write int again. The box already exists. The old value goes out and the new value goes in. If you write int umur = 18; a second time, javac rejects it, because a box named umur already exists.

println(umur) has no quotes. Java opens the umur box and shows its content. If you write println("umur") with quotes, you see the text umur, not 18.

The naming rule. The second word starts with a capital letter and there is no space, as in sukaKopi. This style is called camelCase. And remember, Java treats capital and small letters as different. umur and Umur are two different names.


6. Moving a Value to Another Box

What happens when you move the content of one box to another box?

From a small box to a big box: safe. The content of an int box can go into a double box. It is like pouring water from a glass into a bucket.

JAVA
void main() {
    int jumlah = 3;
    double total = jumlah;
    IO.println(total);
}
TEXT
3.0

From a big box to a small box: rejected. It is like pouring a bucket into a glass: the water spills.

JAVA
int harga = 9.99;
TEXT
Kotak.java:2: error: incompatible types: possible lossy conversion from double to int
    int harga = 9.99;
                ^
1 error

"Possible lossy conversion" means: something could be lost. The number 9.99 cannot go into an int box, because the digits after the decimal point would be thrown away.

DirectionExampleResult
Small to bigint to doubleSafe
Big to smalldouble to intRejected by javac

7. Three Beginner Traps

Trap one: writing a decimal number with a comma.

In Indonesia, and in many other countries, people write decimals with a comma: 165,5. In Java code, a decimal number must use a dot: 165.5. With a comma, javac gets confused:

TEXT
Kotak.java:2: error: <identifier> expected
    double tinggi = 165,5;
                        ^
1 error

The message does not mention the word "comma". So if you see a strange error on a line with a decimal number, check the comma first.

Trap two: char with double quotes.

Single quotes are for one character. Double quotes are for a string. "R" is a string, and a string cannot go into a char box.

TEXT
Kotak.java:2: error: incompatible types: String cannot be converted to char
    char inisial = "R";
                   ^
1 error

Trap three: a huge number in a long box.

JAVA
long penduduk = 8000000000;
TEXT
Kotak.java:2: error: integer number too large
    long penduduk = 8000000000;
                    ^
1 error

Java treats every whole number you write as an int, and eight billion does not fit in an int. Add a capital L at the end, so Java knows it is a long.

A bonus: you can use underscores to separate thousands, so long numbers are easier to read.

JAVA
long penduduk = 8_000_000_000L;

Summary

  1. A variable is a box with a label. It stores a value, and the value can be changed.
  2. Every box has a data type: int for whole numbers, double for decimals, boolean for true or false, char for one character.
  3. javac checks the content of every box. Small box to big box is safe. The other direction is rejected.

Challenge

Look at this code:

JAVA
void main() {
    int a = 7;
    double b = a;
    IO.println(b);
}

What is the output?

  • A. 7
  • B. 7.0
  • C. An error.

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


Conclusion & What's Next

Your program now has a memory. You can create a box, give it a name, fill it, and change its content. You also know why Java is strict: a wrong value is found before the program runs.

But our boxes can only store numbers, true or false, and one letter. In Part 4 we meet strings, which are text in Java. Picture a necklace of letter beads that cannot be changed. Why not?

See you in Part 4. 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 2Your First Program: Hello, Worldpart 4 →Strings

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