Primitive Data Types

Java has eight primitive types for simple values: whole numbers, decimals, single characters and true/false. They're fast and compact, and each has a fixed size and range. Knowing them helps you pick the right type and avoid surprises like overflow and rounding errors.

The eight primitives

TypeSizeHoldsExample
byte8 bits−128 to 127byte b = 100;
short16 bits−32,768 to 32,767short s = 20000;
int32 bitsabout ±2.1 billionint i = 1_500_000;
long64 bitsabout ±9.2 quintillionlong l = 8_000_000_000L;
float32 bits~7 significant digitsfloat f = 2.5f;
double64 bits~15–16 significant digitsdouble d = 2.5;
char16 bitsone UTF-16 characterchar c = 'A';
boolean—true or falseboolean ok = true;

In everyday code, use int for whole numbers, long when they may exceed two billion, double for decimals, and boolean for flags.

Limits and overflow

JAVA
void main() {
    System.out.println(Integer.MAX_VALUE);
    System.out.println(Integer.MAX_VALUE + 1);
    System.out.println(Long.MAX_VALUE);
}
Output
2147483647
-2147483648
9223372036854775807

Going past the maximum doesn't cause an error — the value silently wraps around to the minimum. Use long for large counts, or Math.addExact, which throws an exception on overflow.

long and float literals

JAVA
void main() {
    long population = 8_100_000_000L;
    float temperature = 21.5f;
    System.out.println(population);
    System.out.println(temperature);
}
Output
8100000000
21.5

Whole-number literals are int by default, so large ones need an L suffix. Decimal literals are double by default, so float values need an f.

Decimals aren't exact

JAVA
void main() {
    System.out.println(0.1 + 0.2);
    System.out.println(1.0 - 0.9);
}
Output
0.30000000000000004
0.09999999999999998

double stores numbers in binary, and values like 0.1 can't be represented exactly. That's fine for measurements and science, but for money use BigDecimal (lesson 11) or whole cents in a long.

char

JAVA
void main() {
    char letter = 'J';
    char next = (char) (letter + 1);
    System.out.println(letter);
    System.out.println(next);
    System.out.println((int) letter);
}
Output
J
K
74

A char uses single quotes and is stored as a number (its Unicode code), so you can do arithmetic on it. Strings use double quotes: 'A' is a char, "A" is a String.

boolean

JAVA
void main() {
    boolean isAdult = 20 >= 18;
    boolean hasTicket = false;
    System.out.println(isAdult);
    System.out.println(isAdult && hasTicket);
}
Output
true
false

Unlike some languages, Java's boolean isn't a number: you can't use 0 or 1 in its place.

Primitives vs reference types

Everything that isn't one of these eight is a reference type: String, arrays, and every class. A primitive variable holds the value itself; a reference variable holds a reference (an address) to an object elsewhere in memory. The difference matters when copying and comparing — lessons 9 and 23 return to it. Each primitive also has a wrapper class (Integer, Double…) used with collections (lesson 38).

Default values

Fields in classes get default values (0, 0.0, false, '\u0000', or null for references). Local variables inside methods get no default and must be assigned before use, as you saw in lesson 5.

Try it yourself

Print the range of each integer type.

Try it yourself · Main.java
Edit the code, copy it into Main.java and run java Main.java (Java 25+). How to set up Java
Output
byte:  -128 to 127
short: -32768 to 32767
int:   -2147483648 to 2147483647
long:  -9223372036854775808 to 9223372036854775807