A data type defines HOW MUCH MEMORY is required to store data and what kind of data a variable can hold — such as numbers, characters, or logical values.
Computers can’t directly store decimal numbers like 0 to 9 — they only understand binary (0s and 1s).
So, whenever we store a value like 15, the computer automatically converts it to binary, e.g., 15 → 1111.
This binary data is then stored using transistors — tiny electronic switches that represent:
0 → OFF state
1 → ON state
Each 0 or 1 is called a bit (short for binary digit), and it's the smallest unit of data in computing.
| Term | Value |
|------------------ |---------------------------------------|
| 1 nibble | 4 bits |
| 2 nibbles = 1 byte | 8 bits |
| Integer | 32 bits → 4 bytes |
| Max value in nibble | `1111` in binary = `15` in decimal |
| 1 byte range | 0 to 255 (`00000000` to `11111111`) |
Java has two categoriesS of data types:
| Type | Details |
|----------------|---------------------------------------------------------------------|
| Primitive | Stores actual values in memory (like `int`, `char`, `boolean`) |
| Non-Primitive | Stores references to memory locations (like `String`, `Array`)
Java provides 8 primitive types.
PRIMITIVES store ACTUAL VALUE directly in memory.
| Data Type | Default Value | Size in Bytes | Range |
| --------- | ------------- | ------------- | ------------------------------ |
| `byte` | 0 | 1 byte | -128 to 127 |
| `short` | 0 | 2 bytes | -32,768 to 32,767 |
| `int` | 0 | 4 bytes | -2³¹ to 2³¹-1 |
| `long` | 0L | 8 bytes | -2⁶³ to 2⁶³-1 |
| `float` | 0.0f | 4 bytes | ±3.4e38 (6–7 digits precision) |
| `double` | 0.0d | 8 bytes | ±1.8e308 (15 digits precision) |
| `char` | '\u0000' | 2 bytes | Unicode characters |
| `boolean` | false | \~1 byte\* | true or false |
Non-primitive types STORE MEMORY ADDRESS (references), not actual values. They point to OBJECTS stored in the HEAP MEMORY.
These include:
- Classes
- Arrays
- Interfaces
- Strings
- Enums
| Feature | Primitive Type | Non-Primitive Type |
|--------------------------|---------------------------|-------------------------------|
| Stores actual value | Yes | No (stores reference) |
| Includes | `int`, `char`, `float` | `String`, `Array`, `Class` |
| Memory location | Stack | Heap |
| Default value | 0, false, etc. | `null` |
| Mutable | No (immutable) | Depends on the class |
int age = 30;
float pi = 3.14f;
char grade = 'A';
boolean isPassed = true;
## Notes
- Use int by default for whole numbers and double for decimal numbers.
- Use float only when memory is a concern and precision is less important.
- char is used to store a single character (in Unicode).
- boolean is not numeric — it stores logical values (`true` or `false`). - `byte`, `short`, `int`, `long`, `float`, `double`, `char`, `boolean`
- To maintain platform independence and consistency across different machines .
- `char`- `'\u0000'` (null character); boolean- false
- Java doesn't define a specific bit size for `boolean`, but logically it's 1 bit. Actual memory used depends on JVM.
- Yes, because `char` is internally stored as a Unicode integer value (2 bytes). Implicit casting is allowed.
- `float` is 32-bit and less precise (6–7 decimal digits), `double` is 64-bit and more precise (15 digits). Use `float` when memory is a constraint.
- Compilation error: “possible lossy conversion”. You must explicitly cast it: `int x = (int) 3.14;`
- Changing one data type to another. Implicit (widening) and explicit (narrowing) casting.
- To support Unicode characters, which need more than 1 byte.
- `-2,147,483,648` to `2,147,483,647` (`-2^31 to 2^31 - 1`). Exceeding it causes overflow.
null: reference to nothing (objects)
0: numeric zero (int/float)
false: boolean value.