INSTACK 360

Java Data Types & Variables

Deep dive into Java's strongly typed variable system, primitive data types, and reference types.

Beginner 5 min read

Variables in Java

A variable provides us with named storage that our programs can manipulate. Java is a strongly typed language, meaning every variable must be declared with a data type before it can be used.

int age = 25;
String name = "Alice";

Primitive Data Types

There are 8 primitive data types supported by Java. Primitive types are predefined by the language and named by a keyword.

Data TypeSizeDescription
byte1 byteStores whole numbers from -128 to 127
short2 bytesStores whole numbers from -32,768 to 32,767
int4 bytesStores whole numbers from -2,147,483,648 to 2,147,483,647
long8 bytesStores huge whole numbers. Suffix with 'L'
float4 bytesStores fractional numbers. Suffix with 'f'
double8 bytesStores fractional numbers with double precision
boolean1 bitStores true or false values
char2 bytesStores a single character/letter or ASCII value

Reference Data Types

Reference variables are created using defined constructors of the classes. They are used to access objects. Examples include String, Arrays, and user-defined classes.