Java Data Types

Data types are very important in Java because it is a strongly typed language. All operations are type-checked by the compiler for type compatibility. Strong type checking helps prevent errors. All variables, expressions, and values have a type.

Java contains two general categories of built-in data types:

  • Object-oriented
  • Primitive (non-object-oriented)

Object-oriented types are defined by classes (will be discussed later).

Primitive types are not objects in an object-oriented sense, but rather, normal binary values.

There are 8 primitive types.

Integers

Java defines four integer types which are byte, short, int, and long. All of the integer types are signed positive and negative values. Java does not support unsigned (positive-only) integers.

Type Width in Bits Range 
byte8–128 to 127
short16–32,768 to 32,767
int32–2,147,483,648 to 2,147,483,647
long64–9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
Integer types

Floating-Point

The floating-point types represents numbers that have fractional part.

There are 2 floating types – float and double.

TypeWidth in Bits Precision
Float32single-precision numbers
Double64double-precision numbers
Floating-point types

Characters

In Java characters use Unicode.

TypeWidth in Bits Range
CharactersUnsigned 160 to 65,536

Since char is an unsigned 16-bit type, character variables can be handled like integers and it is possible to perform various arithmetic manipulations on a char variable.

The Boolean Type

The boolean type represents true/false values. Java defines the values true and false using the reserved words true and false.