Working with Primitive Data Types
1. Declaring Integer Types
| Type | Size | Range | Default |
byte | 8-bit | -128 to 127 | 0 |
short | 16-bit | -32,768 to 32,767 | 0 |
int | 32-bit | ±2.14 billion | 0 |
long | 64-bit | ±9.2 quintillion | 0L |
Example: Integer literals
byte b = 100;
int decimal = 1_000_000; // underscore separator
int hex = 0xFF; // hexadecimal
int binary = 0b1010_0001; // binary
long big = 9_999_999_999L; // L suffix required
2. Working with Floating-Point Types
| Type | Size | Precision | Suffix |
float | 32-bit IEEE 754 | ~7 digits | f / F |
double | 64-bit IEEE 754 | ~15 digits | d (optional) |
Example: Float vs double
float price = 19.99f; // f required
double pi = 3.14159265358979;
double sci = 1.5e3; // 1500.0
double hexFloat = 0x1.8p3; // 12.0
Warning: Never use float/double for money — use BigDecimal.
3. Using Character Type
| Form | Example | Notes |
| Literal | 'A' | Single quotes |
| Unicode escape | '\u0041' | 16-bit code unit |
| Numeric value | (char) 65 | Same as 'A' |
| Escape sequences | '\n' '\t' '\\' '\'' | Special characters |
4. Understanding Boolean Type
| Property | Value |
| Type | boolean |
| Values | true, false |
| Default | false |
| Size | JVM-dependent (typically 1 byte stored, 4 in arrays) |
5. Understanding Default Values
| Type | Default (instance/static fields) |
| Numeric primitives | 0 / 0L / 0.0 |
boolean | false |
char | '\u0000' |
| Reference types | null |
Note: Local variables have NO default — must be explicitly assigned before use.
6. Working with Literals
| Literal | Example |
| Integer | 42, 0x2A, 0b101010, 052 (octal) |
| Long | 42L |
| Float | 3.14f |
| Double | 3.14, 3.14d |
| Char | 'A', '\u0041' |
| String | "hello" |
| Underscore separator | 1_000_000 (Java 7+) |
7. Understanding Type Casting
| Cast | Behavior | Example |
| Widening (implicit) | No data loss | int i = 10; long l = i; |
| Narrowing (explicit) | Possible data loss | int i = (int) 3.7; // 3 |
| Promotion | byte/short → int in expressions | byte a=1,b=2; int c = a+b; |
8. Using Wrapper Classes
| Primitive | Wrapper | Useful Methods |
int | Integer | parseInt, valueOf, compare, MAX_VALUE |
long | Long | parseLong, toBinaryString |
double | Double | parseDouble, isNaN, isInfinite |
boolean | Boolean | parseBoolean, logicalAnd |
char | Character | isDigit, isLetter, toUpperCase |
9. Understanding Autoboxing and Unboxing
| Operation | Direction | Example |
| Autoboxing | primitive → wrapper | Integer x = 5; |
| Unboxing | wrapper → primitive | int y = x; |
Warning: Unboxing a null wrapper throws NullPointerException. Avoid wrappers in tight loops (boxing overhead).
10. Comparing Primitives vs Wrapper Classes
| Aspect | Primitive | Wrapper |
| Storage | Stack/inline | Heap object |
| Default | 0/false | null |
| Equality | == compares value | == compares reference, use .equals() |
| Use in collections | Not allowed | Required (List<Integer>) |
| Performance | Faster, less memory | Object overhead |