Java Fundamentals

Variables & Primitive Data Types

15 min Lesson 3 of 14

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:

// declaration (no value yet — uses a default if it is a field, or causes a compile error if local) int score; // declaration + initialisation in one line int score = 100;

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¹⁸. Suffix L is required for long literals.
byte temperature = 36; short year = 2024; int population = 8_000_000_000 / 1000; // underscore separators aid readability long distanceKm = 9_460_730_472_580_800L; // the L marks a long literal
Use 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. Suffix f or F required.
  • double — 64 bits, about 15 significant decimal digits. The everyday decimal type. No suffix needed; a literal like 3.14 is already a double.
float taxRate = 0.15f; // must have the f suffix double pi = 3.141592653589793; double gravity = 9.81;
Never use floating-point for money. 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 grade = 'A'; char newline = '\n'; // escape sequences work char heart = '♥'; // Unicode escape: ♥
A 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.

boolean isLoggedIn = true; boolean hasPermission = false; boolean isAdult = (age >= 18); // comparison produces a boolean

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, long0
  • float, double0.0
  • char'' (the null character)
  • booleanfalse
Local variables have no default. If you declare 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:

// Integer literals in different bases int decimal = 255; int hex = 0xFF; // 0x prefix int binary = 0b1111_1111; // 0b prefix (Java 7+) int octal = 0377; // leading 0 // Underscore separators (Java 7+) — ignored by the compiler, great for readability int million = 1_000_000; long creditCard = 1234_5678_9012_3456L; // Floating-point literals double sci = 1.5e10; // scientific notation: 1.5 × 10^10 float small = 3.14f; // String is NOT a primitive — it is a class — but for reference: // String name = "Alice"; <-- double quotes // char letter = 'A'; <-- single quote (char only!)

A Complete Example

Here is a small self-contained program that declares one variable of each primitive type and prints it:

public class PrimitiveDemo { public static void main(String[] args) { byte b = 100; short s = 30_000; int i = 2_000_000; long l = 9_000_000_000L; float f = 3.14f; double d = 2.718281828; char c = 'J'; boolean flag = true; System.out.println("byte: " + b); System.out.println("short: " + s); System.out.println("int: " + i); System.out.println("long: " + l); System.out.println("float: " + f); System.out.println("double: " + d); System.out.println("char: " + c); System.out.println("boolean: " + flag); } }

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.