Data Types

What Are the 8 Primitive Data Types in Java?

Java has eight primitive data types: byte, short, int, long, float, double, char and boolean. Here is what each one holds, its size and an example.

On this page
  1. Quick answer
  2. The eight types
  3. Default values
  4. How they differ from other Java types
  5. A floating-point surprise

Quick answer

Java has eight primitive data types: byte, short, int, long, float, double, char and boolean. Each one holds a single simple value, and the language defines what each type can store.

The eight types

Type What it holds Example
byte Small whole numbers, 8-bit signed, from -128 to 127 byte level = 100;
short Whole numbers, 16-bit signed short year = 2026;
int Whole numbers, 32-bit signed. The usual choice for integers int count = 42;
long Larger whole numbers, 64-bit signed long views = 9000000000L;
float Decimal numbers, 32-bit floating point float ratio = 0.5f;
double Decimal numbers, 64-bit floating point. The default for decimal literals double price = 19.99;
char A single 16-bit UTF-16 code unit, usually one character char grade = 'A';
boolean true or false boolean ready = true;

The L suffix marks a long literal, and the f suffix marks a float literal. Without the f, a decimal literal is read as a double, so 0.5 on its own cannot be assigned to a float. A whole number too large for int needs the L suffix, or the compiler rejects it.

Default values

Fields in a class get default values if you do not set them. Numeric fields default to 0, a boolean defaults to false, and a char defaults to the null character, written '\u0000'.

Local variables are different. Java requires a local variable to be assigned a value before you read it, and the compiler checks this.

How they differ from other Java types

Java’s primitive types hold values directly. String, arrays and classes are non-primitive, so they are objects. For a side-by-side comparison of the two groups, see primitive vs non primitive data types.

Each primitive also has a wrapper class, such as Integer for int. Java uses wrapper classes when a value has to be an object, for example in generic collections. A list of int is not allowed, so a list of Integer is used instead.

A floating-point surprise

Decimal numbers in a double are stored in binary, so some values are only approximate. In Java, 0.1 + 0.2 does not give exactly 0.3. It gives 0.30000000000000004. For money or other exact values, avoid double and use a type designed for exact decimals, such as BigDecimal.

The complete guide to data types

Primitive vs. Non-Primitive Data Types
Read the guide

Keep exploring

Guides from other topics

All topics →