Variables in Java: Primitive Data Types vs Reference or Non-Primitive Types

Variables in Java are used to hold a certain value. They are declared as a variable type and are given a name. Then they are assigned a value which is then stored in a memory location.

Primitive Variables

There are eight different types of primitive variables in Java. They hold different data types and are allocated a pre-defined spaced in the memory.

Data Type Size Minimum Maximum Default
boolean 1 bit false
char 2 bytes ‘\u0000\
byte 1 byte -128 127 0
short 2 bytes -32,768 32,768 0
int 4 bytes -2,147,483,648 2,147,483,647 0
long 8 bytes -9.22337E+18 9.22337E+18 0
float 4 bytes 0.0f
double 8 bytes 0.0d


You can set values as:

byte b = 1;
short s = 20;
int i = 20,000;
long l = 200L;
float f = 100.2f;
double d = 100.2d;
char c = ‘a’;

Note how double and float values are declared so that the compiler knows what type of data type they are. They are known as literals.

Non-Primitive or Reference Variables

These are data types that are pre-defined objects in java or are created by the user as an object. They must be defined and initiated to be used. For example: String, Array, enum, interface and user-defined class.

Using Strings



There’s a lot more that you can do with strings with helper classes and methods like concatenating, comparing etc.