Data Types
Java is a statically typed language, meaning every variable must have a declared type, and that type can never change. Java's types fall into two broad categories: primitive types and reference types.
The Eight Primitive Types
Primitive types hold simple values directly in memory — they are not objects. Java defines exactly eight of them:
| Type | Size | Range / Example |
|---|---|---|
byte | 8-bit | -128 to 127 |
short | 16-bit | -32,768 to 32,767 |
int | 32-bit | -2,147,483,648 to 2,147,483,647 |
long | 64-bit | -9.2 quintillion to 9.2 quintillion |
float | 32-bit | Single-precision decimal, e.g. 3.14f |
double | 64-bit | Double-precision decimal, e.g. 3.14159 |
char | 16-bit | A single character, e.g. 'A' |
boolean | 1 bit (conceptually) | true or false |
byte smallNumber = 100;
long population = 8000000000L;
float temperature = 36.6f;
char grade = 'A';
boolean isPassed = true;
Primitive vs Reference Types
A primitive type stores its actual value directly. A reference type stores a reference (an address) pointing to an object elsewhere in memory. Classes, arrays, and interfaces are all reference types.
String is the most common example of a reference type — even though it's used constantly like a
primitive, it's actually an object under the hood:
String greeting = "Hello";
String starts with a capital letter, unlike primitive types (int, double, etc.) which are always lowercase. That capitalization is your visual clue that something is a class, not a primitive.Default Values
Instance and static fields are automatically given a default value if you don't initialize them yourself. Local variables inside methods do not get a default — you must assign one before using them.
| Type | Default value |
|---|---|
byte, short, int, long | 0 |
float, double | 0.0 |
char | ' ' (null character) |
boolean | false |
Reference types (e.g. String) | null |
Literal Suffixes
Some literal values need a suffix to tell the compiler exactly which type you mean, since numeric literals default
to int or double:
| Suffix | Meaning | Example |
|---|---|---|
L | Marks a literal as long | 8000000000L |
f | Marks a literal as float | 36.6f |
d | Marks a literal as double (optional — it's the default) | 36.6d |
L suffix, a large whole number literal is treated as int first and may not fit, causing a compile error even when assigning it to a long variable.