Java Data Types & Variables
Deep dive into Java's strongly typed variable system, primitive data types, and reference types.
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 Type | Size | Description |
|---|---|---|
| byte | 1 byte | Stores whole numbers from -128 to 127 |
| short | 2 bytes | Stores whole numbers from -32,768 to 32,767 |
| int | 4 bytes | Stores whole numbers from -2,147,483,648 to 2,147,483,647 |
| long | 8 bytes | Stores huge whole numbers. Suffix with 'L' |
| float | 4 bytes | Stores fractional numbers. Suffix with 'f' |
| double | 8 bytes | Stores fractional numbers with double precision |
| boolean | 1 bit | Stores true or false values |
| char | 2 bytes | Stores 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.