Skip to content

Data Types – Different Kinds of Data in Java

Seanti edited this page Mar 24, 2025 · 1 revision

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.

1. Java’s Four Main Data Types

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)

2. Using Data Types in Java

Let’s see how to create and use variables with these data types.

Example 1: Whole Numbers (int) → Gumball Machine

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.


Example 2: Decimal Numbers (double) → Water Bottle

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.


Example 3: True or False (boolean) → Light Switch

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.


Example 4: Text (String) → Sticky Note

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.


3. Mixing Different Data Types

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);

Example Output:

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!


4. Why Are Data Types Important?

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.

5. Quick Recap – Data Type Cheat Sheet

Data Type Analogy Stores
int Gumball machine Whole numbers
double Water bottle Decimal numbers
boolean Light switch true or false
String Sticky note Text

6. What’s Next?

Now that we understand data types, we’ll learn how to use operators to do calculations! 🚀

Clone this wiki locally