Data Types

The term data type refers to the type of data that can be stored in a variable. Sometimes, Java is called a “strongly typed language” because when you declare a variable, you must specify the variable’s type. Then the compiler ensures that you don’t try to assign data of the wrong type to the variable. The following example code generates a compiler error:

int x;

x = 3.1415;

Because x is declared as a variable of type int (which holds whole numbers), you can’t assign the value 3.1415 to it.

Java distinguishes between two kinds of data types: primitive types and reference types. Primitive types are the data types defined by the language itself. By contrast, reference types are types defined by classes in the Java application programming interface (API) or by classes you create rather than by the language itself.

Java defines eight primitive types:

Type

Explanation

int

A 32-bit (4-byte) integer value

short

A 16-bit (2-byte) integer value

long

A 64-bit (8-byte) integer value

byte

An 8-bit (1-byte) integer value

float

A 32-bit (4-byte) floating-point value

double

A 64-bit (8-byte) floating-point value

char

A 16-bit character using the Unicode encoding scheme

boolean

A true or false value

These primitive data types are discussed in their respective sections.

Get Java For Dummies Quick Reference now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.