-
Notifications
You must be signed in to change notification settings - Fork 0
Data Types – Different Kinds of Data in Java
Just like in real life, Java needs different types of containers to store different kinds of information.
Imagine your kitchen:
- A gumball machine → Only holds whole gumballs (no half gumballs!).
- A water bottle → Holds water, which can be full, half-full, or empty.
- A light switch → Can be ON or OFF (only two choices).
- A sticky note → Holds words or sentences.
| Data Type | What It Stores | Example | Analogy |
|---|---|---|---|
int |
Whole numbers | 5, 100, -3 |
Gumball machine (only full gumballs) |
double |
Decimal numbers | 3.14, 0.5, -2.75 |
Water bottle (can be full or half-full) |
boolean |
True/False values | true, false |
Light switch (only ON or OFF) |
String |
Text (words/sentences) | "Hello", "Java is fun!" |
Sticky note (holds words) |
Let’s see how to create and use variables with these data types.
An int is like a gumball machine—it can only store full gumballs (no halves!).
int gumballs = 10;
System.out.println("I have " + gumballs + " gumballs.");➡️ int gumballs = 10; → Stores 10 whole gumballs.
A double is like a water bottle—it can hold a whole or partial amount of water.
double waterAmount = 1.5;
System.out.println("My water bottle has " + waterAmount + " liters of water.");➡️ double waterAmount = 1.5; → Stores 1.5 liters of water.
A boolean is like a light switch—it can only be ON (true) or OFF (false).
boolean lightIsOn = true;
System.out.println("Is the light on? " + lightIsOn);➡️ boolean lightIsOn = true; → The switch is ON.
A String is like a sticky note—it holds words or sentences.
String note = "Don't forget to do homework!";
System.out.println("Sticky note says: " + note);➡️ String note = "Don't forget to do homework!"; → Stores a text message.
We can combine different data types to create a message!
String name = "Jake"; // Sticky note
int gumballs = 20; // Gumball machine
double waterAmount = 2.5; // Water bottle
boolean lightIsOn = false; // Light switch
System.out.println(name + " has " + gumballs + " gumballs.");
System.out.println("His water bottle has " + waterAmount + " liters of water.");
System.out.println("Is the light on? " + lightIsOn);Jake has 20 gumballs.
His water bottle has 2.5 liters of water.
Is the light on? false
➡️ We combined numbers, text, and boolean values into a readable sentence!
Think about it:
- You wouldn’t put water inside a gumball machine, right?
- You can’t store words in a light switch!
- Java needs to know what kind of data you're using so it can handle it correctly.
| Data Type | Analogy | Stores |
|---|---|---|
int |
Gumball machine | Whole numbers |
double |
Water bottle | Decimal numbers |
boolean |
Light switch |
true or false
|
String |
Sticky note | Text |
Now that we understand data types, we’ll learn how to use operators to do calculations! 🚀