Variables and Data Types
Imagine you are moving into a new house. You have many things to store:
- Your age
- Your name
- Your exam marks
- A book
- A car key
You cannot treat all of these things exactly the same way. Java has the same idea. A computer needs to know: What kind of data are we storing? and Where can we find that data?
This leads us to two fundamental concepts: Variables and Data types.
What Is a Variable?
A variable is a named location used by a program to store a value.
int age = 21;Here:
int→ data typeage→ variable name21→ value=→ assignment operator
You can think of a variable as a labeled box. The label allows your program to refer to the stored value.
Why Does Java Need Data Types?
Consider two pieces of information: 21 and "Hello". They are both values, but they represent completely different kinds of information. 21 is a number. "Hello" is text. Java therefore uses data types to tell the compiler what kind of value a variable is designed to hold.
Java Data Types: The Big Picture
Java types can broadly be divided into:
- Primitive Types
- Reference Types
Let's understand the difference using a real-world analogy.
Primitive Types: The Physical Box
Imagine you have a physical box containing a number 25. The box directly contains the value. Primitive variables are similar in the sense that they represent simple values directly.
Examples include:
int age = 25;double price = 99.99;boolean isStudent = true;char grade = 'A';The Eight Primitive Data Types
| Type | Used For | Example |
|---|---|---|
byte | Small whole numbers | byte x = 10; |
short | Small-to-medium whole numbers | short x = 1000; |
int | Whole numbers | int age = 21; |
long | Large whole numbers | long population = 8000000000L; |
float | Decimal numbers | float price = 10.5f; |
double | More precise decimal numbers | double price = 10.5; |
char | A single character | char grade = 'A'; |
boolean | True/false values | boolean active = true; |
Reference Types: The Remote Control Analogy
Now we reach an extremely important concept. Consider a TV remote control. The remote is not the television. It gives you a way to interact with the television.
You can think of a reference variable somewhat like a remote control that lets the program locate and interact with an object.
Student student = new Student();The variable student is a reference variable. It refers to a Student object.
Primitive vs Reference: The Core Difference
Consider:
int age = 21;versus:
Student student = new Student();A useful beginner-level mental model is:
- Primitive: The variable directly holds the value (21).
- Reference: The variable holds a reference (like a pointer or remote control) to where the object actually lives in memory.
A Very Important Point About String
Beginners often think: "String must be primitive because it is used so often." It is not. String is a reference type. String is a class provided by Java.
Quick Comparison
| Feature | Primitive | Reference |
|---|---|---|
| Represents | Simple value | Reference to an object |
| Examples | int, double, boolean | String, arrays, classes |
| Can be null? | No | Yes |
| Main analogy | Physical box | Remote control |