Data types in Java define the type of data a variable can store. They help Java allocate memory efficiently and perform operations correctly.
What is a Data Type?
A data type specifies:
What kind of value a variable can hold
How much memory is required
What operations can be performed on the value
Types of Data Types in Java
Primitive Data Types
Non-Primitive (Reference) Data Types
Primitive Data Types
Java provides 8 primitive data types.
| Data Type | Size | Description | Example |
|---|---|---|---|
| byte | 1 byte | Small integer | byte age = 20; |
| short | 2 bytes | Short integer | short num = 1000; |
| int | 4 bytes | Integer value | int marks = 90; |
| long | 8 bytes | Large integer | long population = 8000000L; |
| float | 4 bytes | Decimal value | float price = 99.99f; |
| double | 8 bytes | Large decimal value | double salary = 50000.50; |
| char | 2 bytes | Single character | char grade = 'A'; |
| boolean | 1 bit | True or False | boolean active = true; |
Non-Primitive Data Types
Non-primitive data types store references to objects.
Examples:
String
Arrays
Classes
Interfaces
Collections
Primitive vs Non-Primitive Data Types
| Feature | Primitive | Non-Primitive |
|---|---|---|
| Stores | Actual value | Reference address |
| Size | Fixed | Variable |
| Created by Java | Yes | User-defined or Java classes |
| Can call methods | No | Yes |
| Example | int, double | String, Array |
Top comments (0)