Data Types in Java Programming Language
In this lesson, we will learn about the Data Types in the Java programming language along with a built-in data types table.
What is Data Type
In the Java programming language, data types specify the type of data that a variable can hold. Each data type requires a different amount of memory and has a specific range of values.
Understanding data types is crucial because it helps you choose the appropriate type for storing and manipulating data. Java supports the following data types given below.
- The short name for Integer data type is int.
- The short name for Float data type is float.
- The short name for Double data type is double.
- The short name for Character data type is char.
Basic Data Types in Java
Let's go through the basic data types in Java with simple explanations and examples.
int (Integer)
- Description: Store whole numbers without decimals.
- Memory Size: 4 bytes.
- Range: -2,147,483,648 to 2,147,483,647.
int guest_count = 20;
float (Float)
- Description: Stores numbers with decimals (upto 7 decimal places).
- Memory Size: 4 bytes.
- Range: −3.4E+38 to 3.4E+38 (meaning -3.4 × 1038 to 3.4 × 1038).
float budget = 5000.00f;
Here f is used to denote a float type value. If no f is used in a decimal number, then the number is treated as double type value.
double (Double)
- Description: Stores numbers with decimals (upto 15 decimal places).
- Memory Size: 8 bytes.
- Range: -1.7E+308 to 1.7E+308 (meaning -1.7 × 10308 to 1.7 × 10308).
double distance = 1263.2514376;
char (Character)
- Description: Stores a single character within single quotes.
- Memory Size: 2 byte.
- Range: 0 to 65535.
char grade = 'A';
Storage Size and Range of Built-in Data Types in Java
| Data Type | Storage Size | Range |
| boolean | JVM-dependent (typically 1 byte) | true or false |
| byte | 1 byte | -128 to 127 |
| char | 2 bytes | 0 to 65535 |
| short | 2 bytes | -32,768 to 32,767 |
| int | 4 bytes | -2,147,483,648 to 2,147,483,647 |
| long | 8 bytes | -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 |
| float | 4 bytes | −3.4E+38 to 3.4E+38 (upto 7 decimal digits) |
| double | 8 bytes | -1.7E+308 to 1.7E+308 (upto 15 decimal digits) |
Although a boolean conceptually represents 1 bit, in practice, it usually takes 1 byte (8 bits) in Java due to memory alignment and JVM constraints.
Test Your Knowledge
Attempt the questions in the exercise to check whether you have understood the lesson clearly.