Java Data Types Explained – Primitive and Non-Primitive
Data Types in Java:
Introduction
In Java programming, data types play a very important role. A data type defines the type of value a variable can store. For example, some variables store numbers, some store characters, and some store true/false values.
Types of Data Types in Java
In Java, data types are mainly divided into two categories:
1. Primitive Data Types in Java
Primitive data types are the basic built-in data types provided by Java and used to store simple values like numbers, characters, and true/false values. They are not objects and store actual values directly in memory, which makes them fast and memory efficient.
Java has 8 primitive data types.
| Data Type | Description | Example |
|---|---|---|
| byte | Stores small integers | byte a = 10; |
| short | Stores slightly larger integers | short b = 200; |
| int | Stores whole numbers | int marks = 85; |
| long | Stores very large numbers | long population = 9000000; |
| float | Stores decimal numbers | float price = 10.5f; |
| double | Stores large decimal values | double pi = 3.14159; |
| char | Stores a single character | char grade = 'A'; |
| boolean | Stores true or false values | boolean pass = true; |
1. byte
-
Used to store small integer numbers.
-
Memory size: 1 byte (8 bits)
-
Range: -128 to 127
Example:
byte age = 25;
2. short
-
Used for storing slightly larger integers than byte.
-
Memory size: 2 bytes (16 bits)
-
Range: -32,768 to 32,767
Example:
short temperature = 120;
3. int
-
Most commonly used integer data type.
-
Memory size: 4 bytes (32 bits)
-
Range: -2,147,483,648 to 2,147,483,647
Example:
int marks = 95;
4. long
-
Used to store very large integer values.
-
Memory size: 8 bytes (64 bits)
-
Range: -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
Example:
long population = 7800000000L;
5. float
-
Used to store decimal numbers (single precision).
-
Memory size: 4 bytes
-
Precision: about 6–7 decimal digits
Example:
float price = 45.75f;
6. double
-
Used for large decimal numbers (double precision).
-
Memory size: 8 bytes
-
Precision: about 15 decimal digits
Example:
double distance = 12345.6789;
7. char
-
Used to store a single character.
-
Memory size: 2 bytes (16 bits)
-
Range: 0 to 65,535 (Unicode characters)
Example:
char grade = 'A';
8. boolean
-
Used to store true or false values.
-
Memory size: 1 bit (logical value)
Example:
boolean isPassed = true;
Primitive Data Types Table
| Data Type | Size | Range / Capacity | Example |
|---|---|---|---|
| byte | 1 byte | -128 to 127 | byte b = 10; |
| short | 2 bytes | -32,768 to 32,767 | short s = 200; |
| int | 4 bytes | -2,147,483,648 to 2,147,483,647 | int i = 500; |
| long | 8 bytes | Very large integers | long l = 10000L; |
| float | 4 bytes | 6–7 decimal digits | float f = 5.5f; |
| double | 8 bytes | 15 decimal digits | double d = 10.25; |
| char | 2 bytes | Unicode characters | char c = 'A'; |
| boolean | 1 bit | true / false | boolean flag = true; |
Non-Primitive Data Types:
Unlike primitive data types, they:
-
Can store multiple values
-
Have methods and properties
-
Are created by the programmer or provided by Java
Types of Non-Primitive Data Types in Java
The most common non-primitive data types are:
Features of Non-Primitive Data Types
Non-primitive data types have the following features:
-
They are created using classes
-
They can store multiple values
-
They support methods and functions
-
They store references instead of actual data
-
They are used to build complex programs

Comments
Post a Comment