Variables & Primitive Data Types
Variables & Primitive Data Types
Every program needs to store data — a user's age, a bank balance, a letter grade. In Java you store data in variables, and every variable has a type that tells the compiler exactly what kind of data it holds. Java is statically typed, which means the type is fixed at compile time and cannot change. This lesson covers Java's eight built-in primitive types — the language's lowest-level value storage units.
What Is a Variable?
A variable is a named slot in memory. Declaring a variable reserves that slot; initialising it stores a value in it. The syntax is:
Java enforces that local variables (those inside a method) must be explicitly initialised before you read them. The compiler will refuse to compile code that might read an uninitialised local variable.
The Eight Primitive Types
Java has exactly eight primitive types, split into four categories:
1. Integer Types
These store whole numbers. The difference is the size of the slot in memory and therefore the range of values that fit.
byte— 8 bits. Range: -128 to 127.short— 16 bits. Range: -32,768 to 32,767.int— 32 bits. Range: -2,147,483,648 to 2,147,483,647. The everyday integer type.long— 64 bits. Range: about -9.2 × 10¹⁸ to 9.2 × 10¹⁸. SuffixLis required forlongliterals.
int by default. Only reach for long when a value could exceed ~2.1 billion (file sizes, timestamps in milliseconds, astronomical distances). Use byte or short only when you are working with binary data or very large arrays where memory is critical.
2. Floating-Point Types
These store numbers with a decimal part using the IEEE 754 standard.
float— 32 bits, about 7 significant decimal digits. SuffixforFrequired.double— 64 bits, about 15 significant decimal digits. The everyday decimal type. No suffix needed; a literal like3.14is already adouble.
0.1 + 0.2 does not equal exactly 0.3 in binary floating point. For financial calculations always use java.math.BigDecimal.
3. Character Type
char stores a single Unicode character in 16 bits (UTF-16). Character literals are enclosed in single quotes.
char is technically an unsigned 16-bit integer (0–65,535) representing a Unicode code point. You can do arithmetic on it, though it is rarely useful: 'A' + 1 gives 66, which is 'B'.
4. Boolean Type
boolean holds exactly one of two values: true or false. It is the result of every comparison and logical expression.
Default Values
When a primitive is declared as a class field (not inside a method) and not explicitly initialised, Java assigns it a safe default:
byte,short,int,long→0float,double→0.0char→' '(the null character)boolean→false
int count; inside a method and then try to print it without assigning a value first, the code will not compile. This is a deliberate safety feature — Java prevents you from accidentally reading garbage.
Literals in Depth
A literal is a value written directly in source code. Java supports several literal formats to make code more readable:
A Complete Example
Here is a small self-contained program that declares one variable of each primitive type and prints it:
Summary
Java's eight primitive types cover every fundamental value category: whole numbers (byte, short, int, long), decimal numbers (float, double), a single character (char), and a truth value (boolean). Each type has a fixed size, a specific range, and a default value for class fields. When in doubt, use int for integers and double for decimals — they are the everyday workhorses of Java arithmetic. In the next lesson you will combine these values with operators and expressions to perform real computations.