Data types in Java are the categories of values that can be stored in variables. They define the size and type of the data that can be manipulated by the program.
There are two types of data types in Java:
- primitive
- non-primitive.
1.Primitive data types are the basic data types that are built-in to the Java language. They are also called simple or atomic data types because they cannot be further divided into smaller parts. There are eight primitive data types in Java: byte, short, int, long, float, double, char and boolean. Each of these data types has a fixed size and range of values that it can store.
2.Non-primitive data types are the data types that are defined by the programmer or by the Java library. They are also called complex or reference data types because they refer to objects that have attributes and methods. Non-primitive data types include classes, interfaces, arrays, strings, etc. They can store complex or dynamic data that can vary in size and structure.
Here is a table that summarizes the main characteristics of the primitive data types in Java:
Data Type | Size | Description | Example |
---|---|---|---|
byte | 1 byte | Stores whole numbers from -128 to 127 | byte b = 10; |
short | 2 bytes | Stores whole numbers from -32,768 to 32,767 | short s = 1000; |
int | 4 bytes | Stores whole numbers from -2,147,483,648 to 2,147,483,647 | int i = 100000; |
long | 8 bytes | Stores whole numbers from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 | long l = 1000000000L; |
float | 4 bytes | Stores fractional numbers with up to 6-7 decimal digits | float f = 3.14f; |
double | 8 bytes | Stores fractional numbers with up to 15 decimal digits | double d = 3.14159; |
char | 2 bytes | Stores a single character or Unicode value | char c = ‘A’; |
boolean | 1 bit | Stores true or false values | boolean b = true; |