Master Core Java Concepts!
Subscribe to Ram N Java for clear explanations, practical code examples, and essential Java tutorials!
Subscribe Now on YouTubeJava Data Types Comparison Table
In Java, choosing the right data type is crucial for program efficiency and optimal memory usage. Every primitive data type has predefined properties including its default value, memory footprint, bit-width, and permissible value range.
Complete Comparison Table
| Data Type | Default Value | Size (Bytes) | Bit Size | Range / Value | Syntax Example |
|---|---|---|---|---|---|
| boolean | false | — | 1 bit | true or false | boolean flag = true; |
| char | \u0000 | 2 bytes | 16 bits | 0 to 65,535 | char letter = 'A'; |
| byte | 0 | 1 byte | 8 bits | -128 to 127 | byte b = 100; |
| short | 0 | 2 bytes | 16 bits | -32,768 to 32,767 | short s = 10000; |
| int | 0 | 4 bytes | 32 bits | -2^31 to 2^31 - 1 | int count = 50000; |
| long | 0L | 8 bytes | 64 bits | -2^63 to 2^63 - 1 | long distance = 100000L; |
| float | 0.0f | 4 bytes | 32 bits | IEEE 754 float | float pi = 3.14f; |
| double | 0.0d | 8 bytes | 64 bits | IEEE 754 double | double price = 99.99d; |
Key Takeaways
✔️ Default Literals: Whole numbers default to int, and decimals default to double unless suffixes (L, f) are appended.
✔️ Unsigned Character: char uses 16 bits to hold Unicode code points ranging from 0 up to 65,535.
✔️ Memory Awareness: Using appropriate types like byte or short in large arrays helps conserve substantial memory.
No comments:
Post a Comment