Variables and Data Types
Site Admin
· 11 Sep 2026
· 12 views
What Is a Variable?
A variable is a named box in memory that holds a value while the program runs. Every variable in Java has a declared type, which decides how much memory it uses and which operations are allowed on it.
Declaring and Assigning
int count; // declaration
count = 10; // assignment
int total = 5; // declaration + assignment in one step
double price = 99.5;
char grade = 'A';
boolean isDone = true;
String name = "Anjali";The Eight Primitive Types
| Type | Size | Example |
|---|---|---|
| byte | 1 byte | 127 |
| short | 2 bytes | 32000 |
| int | 4 bytes | 2000000 |
| long | 8 bytes | 10000000000L |
| float | 4 bytes | 3.14f |
| double | 8 bytes | 2.71828 |
| char | 2 bytes | 'A' |
| boolean | 1 bit | true/false |
Primitives vs Reference Types
- Primitives store the actual value directly.
- Reference types (classes, arrays, interfaces) store a reference to an object stored on the heap.
Stringis a reference type.
int a = 5;
int b = a; // b gets a copy of the value 5
String x = "hello";
String y = x; // x and y reference the same objectLocal Variables Must Be Initialised
int n;
System.out.println(n); // compile error: n may not be initialised


- Java is statically typed: every variable has a fixed type.
- Eight primitives cover numbers, characters and booleans.
- Use
Landfsuffixes for long and float literals.