## Organizing Research Approach Colis, Cymon Joshua S.
[BS IT 1B]
- The user's request is to research all Java data types, indicating a need for comprehensive, structured
information.
</think>### Overview of Java Data Types
Java has two main categories of data types: **primitive types** (built-in and immutable) and
**reference types** (objects that refer to instances in memory). Primitive types store values directly,
while reference types store references to objects. Below, I'll detail all standard Java data types, including
their sizes, ranges, and examples. This covers Java SE up to the latest versions (e.g., Java 21), where no
new primitives have been added beyond the originals.
### Primitive Data Types
These are the basic building blocks. They are not objects and have fixed sizes. Java has 8 primitive types.
| Type | Size (bits) | Range/Description | Default Value | Example Usage |
|-----------|-------------|-------------------|---------------|---------------|
| `byte` | 8 | -128 to 127 (signed integer) | 0 | `byte age = 25;` |
| `short` | 16 | -32,768 to 32,767 (signed integer) | 0 | `short year = 2023;` |
| `int` | 32 | -2,147,483,648 to 2,147,483,647 (signed integer) | 0 | `int count = 100;` |
| `long` | 64 | -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 (signed integer) | 0L |
`long bigNumber = 123456789L;` |
| `float` | 32 | ~1.4e-45 to 3.4e+38 (floating-point, IEEE 754) | 0.0f | `float pi = 3.14f;` |
| `double` | 64 | ~4.9e-324 to 1.8e+308 (floating-point, IEEE 754) | 0.0d | `double precise =
3.1415926535;` |
| `char` | 16 | 0 to 65,535 (Unicode character) | '\u0000' | `char letter = 'A';` |
| `boolean`| 1 (varies) | true or false | false | `boolean isActive = true;` |
- **Notes**:
- Use `long` for large integers (append `L` to literals).
- `float` and `double` are for decimals; `double` is preferred for precision.
- `char` holds single Unicode characters.
- `boolean` is for logical values.
### Reference Data Types
These include classes, interfaces, arrays, and enums. They are objects created from classes and stored as
references (pointers) to heap memory. Unlike primitives, they can be null and have methods.
- **Classes**: User-defined or built-in (e.g., `String`, `Integer`).
- Example: `String name = "Java";` (wrapper for `char` arrays).
- **Interfaces**: Abstract types (e.g., `List`, `Runnable`).
- **Arrays**: Fixed-size collections of primitives or objects.
- Example: `int[] numbers = {1, 2, 3};` (array of `int`).
- **Enums**: Special classes for constants.
- Example: `enum Day { MONDAY, TUESDAY; }`
- **Wrapper Classes**: Primitives boxed as objects (e.g., `Integer` for `int`, `Boolean` for `boolean`).
Used for generics and collections.
- Example: `Integer num = 42;` (autoboxing from `int`).
- **Notes**:
- Reference types can be null: `String s = null;`.
- They support inheritance and polymorphism.
- Built-in references include `Object` (root class), `String`, and collections like `ArrayList`.
### Additional Considerations
- **Type Conversion**: Primitives can be cast (e.g., `int` to `long`), but reference types require explicit
casting or inheritance.
- **Generics**: Use with reference types (e.g., `List<String>`).
- **Var Keyword (Java 10+)**: Allows type inference: `var list = new ArrayList<String>();` (still a
reference type).
- **Records (Java 14+)**: Immutable data classes, a form of reference type.
- **No Other Types**: Java doesn't have unsigned primitives or custom primitives; all are covered
above.