The JavaTM Tutorial
Previous Page Lesson Contents Next Page Start of Tutorial > Start of Trail > Start of Lesson Search

Trail: Learning the Java Language
Lesson: The Nuts and Bolts of the Java Language

Data Types

All variables in the Java language must have a data type. A variable's data type determines the values that the variable can contain and the operations that can be performed on it. For example, in the MaxVariables(in a .java source file) program, the declaration int largestInteger declares that largestInteger is an integer (int). Integers can contain only integral values (both positive and negative). You can perform arithmetic operations, such as addition, on integer variables.

The Java programming language has two major categories of data types: primitive and reference(in the glossary).

A variable of primitive type contains a single value of the appropriate size and format for its type: a number, character, or boolean value. For example, the value of an int is an integer, the value of a char is a 16-bit Unicode character, and so on.

Arrays, classes, and interfaces are reference types. The value of a reference type variable, in contrast to that of a primitive type, is a reference to the actual value or set of values represented by the variable. A reference is called a pointer or a memory address in other languages. A reference is like your friend's address: The address is not your friend, but it's a way to reach your friend. A reference type variable is not the array or object itself but rather a way to reach it.

The following table lists, by keyword, all of the primitive data types supported by Java, their sizes and formats, and a brief description of each. The MaxVariables program declares one variable of each primitive type.

Primitive Data Types

Keyword Description Size/Format
(integers)
byte Byte-length integer 8-bit two's complement
short Short integer 16-bit two's complement
int Integer 32-bit two's complement
long Long integer 64-bit two's complement
(real numbers)
float Single-precision floating point 32-bit IEEE 754
double Double-precision floating point 64-bit IEEE 754
(other types)
char A single character 16-bit Unicode character
boolean A boolean value (true or false) true or false


Purity Tip: In other languages, the format and size of primitive data types may depend on the platform on which a program is running. In contrast, the Java language specifies the size and format of its primitive data types. Hence, you don't have to worry about system-dependencies.

Previous Page Lesson Contents Next Page Start of Tutorial > Start of Trail > Start of Lesson Search