computer4kids is now ComputerLeap. Same free lessons, same team. Read why we changed our name

← All lessons · 🧑‍💻 Coding Academy

📦

🎓

⏱ 25 min

Free

Variables & Types in Java

In Java you tell the computer what TYPE each variable is. Learn int, double, boolean and String.

🎯 What you will learn

  • Declare variables with a type
  • Use int, double, boolean, char and String
  • Do math and join strings

👋 Let’s get started!

In Python a box can hold anything. In Java, you label the box with a TYPE first — “this box only holds whole numbers”. It helps catch bugs before the program even runs! 📦

🪜 Step by step

1. 🏷️ Type + name + value

Write the type, then the name, then = and the value.

int lives = 3;
double speed = 2.5;
boolean isHero = true;
String name = "Nova";
2. ➕ Math

Java does math like Python. Careful: int / int drops the decimal!

int a = 7;
int b = 2;
System.out.println(a + b);
System.out.println(a / b);   // 3, not 3.5
3. 🔗 Joining

Use + to join strings and other values.

String player = "Nova";
int score = 120;
System.out.println(player + " scored " + score);

🧪 Try it!

Read the code, guess what it prints, then press 👀 Show output.

Player card

String name = "Nova";
int level = 5;
double health = 87.5;
boolean hasShield = true;

System.out.println("Player: " + name);
System.out.println("Level: " + level);
System.out.println("Health: " + health);
System.out.println("Shield: " + hasShield);

Level up

int level = 1;
level = level + 1;
System.out.println("Level " + level);

📖 Key words

Tap a card to flip it over!

int
Whole number 🔢
double
Number with decimals 🌊
boolean
true or false ✅
String
Text (capital S!) 🔤

🏆 Challenge

Predict the output, then check.

Challenge

int apples = 10;
int eaten = 3;
int left = apples - eaten;
System.out.println("Apples left: " + left);

Knowledge Check Quiz

  1. Which type stores 3.75?
  2. What prints?
  3. Which line is correct Java?
← Previous lesson
Next lesson →