Variables and Data Types in Java
A Quick Thought
When you meet someone new, you naturally remember their name, maybe their age, or what they like to do. Computers can remember things too, but instead of a brain, they use variables.
In this lesson, you’ll show Java how to hold on to small bits of information and reuse them whenever needed. Think of it as giving your program a small memory box where it can keep names, numbers, or favorite activities.
The Project: Your First Digital Introduction Card
Imagine creating a small app that prints a simple “About Me” card every time it runs.
Here’s how it should look:
1 2 3 4 5 6======================== ABOUT ME Name: Ali Khan Age: 15 Hobby: Playing Football ========================
You’ll store each piece of information in a variable — and then print it on the screen, just like last time.
Understanding Variables
A variable is like a labeled container. You give it a name and put a piece of data inside it. Whenever you need that data, you can simply call the variable by name.
Every variable has three things:
- a type, what kind of information it can hold
- a name, your chosen label for that piece of memory
- a value, the actual thing stored inside
Here’s an example:
1 2String name = "Ali"; int age = 15;
In the first line, String means text data, and "Ali" is the value stored inside the variable called name.
In the second, int means a whole number, and 15 is the value stored in age.
Once you’ve created them, you can use those variables anywhere in your program.
🧠 Why This Is Important
In the last lesson, you printed fixed text that never changed.
But real programs aren’t frozen; they adapt.
Maybe you want to print your friend’s info instead of yours — or store hundreds of different users’ details.
Without variables, you’d have to rewrite your code each time.
With variables, you just change the value once, and the rest of the program automatically updates.
It’s how computers keep track of things just like humans do, but far faster.
Common Data Types
When storing information, Java needs to know what kind it is.
If you tell it the wrong type, it gets confused.
Here are a few you’ll use most often:
- String: stores text or words like "Hello" or "Java Learner".
- int: stores whole numbers like 10, 25, or 1000.
- double: stores numbers with decimals like 5.5 or 12.99.
- boolean: stores logical values, either true or false.
For now, we’ll focus on the first two, text and numbers.
Let’s Build It
Let’s write a program that prints an “About Me” card using variables:
1 2 3 4 5 6 7 8 9 10 11 12 13 14public class IntroCard { public static void main(String[] args) { String name = "Ali Khan"; int age = 15; String hobby = "Playing Football"; System.out.println("========================"); System.out.println(" ABOUT ME"); System.out.println(" Name: " + name); System.out.println(" Age: " + age); System.out.println(" Hobby: " + hobby); System.out.println("========================"); } }
The + sign here joins text with variables, letting Java print everything together on one line.
How It Works
- You create variables and store information in them.
- Each print line mixes plain text (inside quotes) with those variables.
- When the program runs, Java replaces the variable names with their actual values.
If you change "Ali Khan" to "Fatima" or "Playing Football" to "Reading Books", the output instantly changes, no extra edits needed.
Learn Together with AI
If you’re using an AI Copilot, you can ask it directly instead of typing everything.
Try saying:
“Write a Java program that stores a person’s name, age, and hobby in variables and prints them as a formatted introduction card.”
The AI will instantly give you the working code.
Then you can ask it to make small changes like:
- add a new field such as “Favorite Food,”
- or turn the border into stars instead of equal signs.
You’ll start seeing how easily your code can evolve.
Practice Time
Now it’s your turn. Create a program that prints Student Details exactly like this:
1 2 3 4 5 6 7======================== STUDENT PROFILE Name: Sara Ahmed Age: 17 Grade: 11 Favorite Subject: Physics ========================
Use variables for each piece of data.
Example Solution
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16public class StudentProfile { public static void main(String[] args) { String name = "Sara Ahmed"; int age = 17; int grade = 11; String subject = "Physics"; System.out.println("========================"); System.out.println(" STUDENT PROFILE"); System.out.println(" Name: " + name); System.out.println(" Age: " + age); System.out.println(" Grade: " + grade); System.out.println(" Favorite Subject: " + subject); System.out.println("========================"); } }
If your output matches what you see above, that means your memory box is working perfectly. You’ve now given your program the ability to store and recall information, a big step forward!
Coming Up Next
You’ve learned how to make Java remember information. Next, you’ll teach it how to calculate with that information. In the following lesson, we’ll build a mini café bill calculator that adds totals and shows you how computers think with numbers.