The data type defines the values that a variable can hold.
In Java, when declaring a variable, we must specify its type.
There are two types of data types in Java:
- Primitive data types
- Reference data types
Primitive Data Types in Java
Primitive data types are the basic data types in Java. They are not objects but are predefined in the Java language and are used as such. These include integers, real numbers, alphabetic characters, and logical values.
All primitive types have a specific amount of memory in bytes to write the corresponding values. Therefore, all primitive data types have a precisely defined range of values that belong to them.
There are eight primitive data types in Java:
byte |
---|
Uses 1 byte (8 bits) for a binary record of integers |
Values of this type are integers in the range -2 ^ 7 to 2 ^ 7-1, ie. [-128, 127] |
The default value is 0 |
Example: byte var1=120, byte var2=-30 |
short |
---|
Uses 2 bytes (16 bits) to write integer binaries |
Values of this type are integers in the range -2 ^ 15 to 2 ^ 15-1, ie. [-32 768, 32 767] |
The default value is 0 |
Example: short var1=12000, short var2=-30000 |
int |
---|
Uses 4 bytes (32 bits) to write integers binary |
Values of this type are integers in the range -2 ^ 31 to 2 ^ 31-1, ie. [-2 147 483 648, 2 147 483 647] |
The default value is 0 |
Example: int var1=120000, int var2=-300000 |
long |
---|
Uses 8 bytes (64 bits) to write integers binary |
Values of this type are integers in the range -2 ^ 63 to 2 ^ 63-1, ie. [-9 223 372 036 854 7765 808, 9 223 372 036 854 775 807] |
The default value is 0L |
Example: long a=120000L, long b=-300000L |
float |
---|
Uses 4 bytes (32 bits) to write decimal numbers |
Values of this type are decimal numbers in the range of approximately -10 ^ 38 to 10 ^ 38 and can have about 8 significant digits. This means that the numbers 54,388 921 499 and 54,388 921 320 would be represented by 54,388 921 |
The default value is 0.0f |
Example: float a=234.5f |
double |
---|
Uses 4 bytes (32 bits) to write decimal numbers |
Values of this type are real numbers in the range of approximately -10 ^ 308 to 10 ^ 308 and can have about 15-16 significant digits. |
The default value is 0.0d |
Example: float a=123.5d |
boolean |
---|
Uses 1 bit to represent two logical values (true and false) |
The default value is false |
This type is most commonly used to store values obtained as a result of calculating relational operations |
Example: boolean isFaster=true |
char |
---|
Uses 2 bytes (16 bits) to write a binary character of the alphabet |
Values of this type are individual characters such as lowercase and uppercase letters (A, a, B, b, C, c …), numbers (0, 1, … 9), punctuation marks (.,!.?, …) and some special characters |
Char data type is used to store any character |
The default value is ‘\u0000’ |
Example: char c=’a’ |
Reference data types
The variables of the reference type (class type) contain references to the data, i.e. the address where the corresponding data can be found. The data is stored in objects.
While a variable of primitive type contains a binary representation of a value, a variable of reference type contains a binary representation of the address of an object.
When we declare a variable with the type of some class, it is just a pointer to the object of that class. It is placed in the Stack, like all other variables, but points to an object located in the Heap part of memory. Heap serves to store objects.

Example:
class User { } public class Test { public static void main(String[] args) { User user = new User(); } }
In the example above, we have declared a user variable of the type User. It points to the object assigned to it, which is located in the Heap part of the memory. So it just contains the reference (address) to data, not the data itself.
To learn more, check out Java tutorials for beginners.