1. The Hook (The "Byte-Sized" Intro)
- In a Nutshell: 8 primitive types: byte (8-bit, -128 to 127), short (16-bit), int (32-bit, default for integers), long (64-bit, add 'L' suffix), float (32-bit, add 'f' suffix), double (64-bit, default for decimals), char (16-bit Unicode), boolean (true/false).
- Non-primitive: String, Arrays, Classes, Interfaces (reference types).
- Default values: Numerics=0, boolean=false, char='\u0000', objects=null.
- Choosing: Use int for whole numbers, double for decimals, String for text.
- Golden rule: Choose smallest type that fits your rangeβsaves memory!
Think of containers. byte = small box (holds -128 to 127 items). int = medium box (billions of items). long = large box (quintillions). double = precision scale (decimals). char = single letter slot. boolean = light switch (on/off). String = text notepad!
2. Conceptual Clarity (The "Simple" Tier)
π‘ The Analogy
- Primitive types: Basic building blocks (Lego pieces)
- byte/short/int/long: Different size containers (cup, bottle, bucket, tank)
- float/double: Rulers (different precision)
- Non-primitive: Complex objects (assembled structures)
3. Technical Mastery (The "Deep Dive")
java
// ===========================================
// 1. INTEGER TYPES
// ===========================================
// byte: 8-bit, -128 to 127
byte age = 25;
byte temperature = -10;
byte maxByte = 127; // Maximum
byte minByte = -128; // Minimum
// short: 16-bit, -32,768 to 32,767
short year = 2024;
short population = 30000;
short maxShort = 32767;
// int: 32-bit, -2.1 billion to 2.1 billion (DEFAULT for integers)
int studentCount = 1000;
int worldPopulation = 8_000_000_000; // Underscores for readability!
int maxInt = 2_147_483_647;
// long: 64-bit, very large numbers (add 'L' suffix)
long distance = 1000000000000L; // Must use L suffix!
long fileSize = 5_000_000_000L;
long maxLong = 9_223_372_036_854_775_807L;
// β BAD: Missing L suffix
long big = 10000000000; // β Compile error! (too large for int)
// β
GOOD: With L suffix
long big = 10000000000L; // β
Works!
// ===========================================
// 2. FLOATING-POINT TYPES
// ===========================================
// float: 32-bit, ~7 decimal digits precision (add 'f' suffix)
float price = 19.99f; // Must use f suffix!
float pi = 3.14f;
float percentage = 85.5f;
// double: 64-bit, ~15 decimal digits precision (DEFAULT for decimals)
double salary = 75000.50;
double precision = 3.14159265358979;
double scientificNotation = 1.23e5; // 1.23 Γ 10^5 = 123000
// β BAD: Missing f suffix for float
float value = 10.5; // β Compile error! (10.5 is double by default)
// β
GOOD: With f suffix
float value = 10.5f; // β
Works!
// ===========================================
// 3. CHARACTER TYPE
// ===========================================
// char: 16-bit Unicode character (single quotes!)
char grade = 'A';
char symbol = '@';
char digit = '7'; // Character '7', not number 7
char unicode = '\u0041'; // Unicode for 'A'
char newline = '\n'; // Escape sequence
// β BAD: Double quotes (that's a String!)
char letter = "A"; // β Compile error!
// β
GOOD: Single quotes
char letter = 'A'; // β
Correct
// ===========================================
// 4. BOOLEAN TYPE
// ===========================================
// boolean: true or false
boolean isActive = true;
boolean hasPermission = false;
boolean isValid = (10 > 5); // Result of comparison
// ===========================================
// 5. NON-PRIMITIVE TYPES (Reference Types)
// ===========================================
// String: Sequence of characters (NOT primitive!)
String name = "Alice";
String message = "Hello, World!";
String empty = ""; // Empty string
// Arrays: Collection of elements
int[] numbers = {1, 2, 3, 4, 5};
String[] names = {"Alice", "Bob", "Charlie"};
// Classes and Objects
Date today = new Date();
Scanner scanner = new Scanner(System.in);
// ===========================================
// 6. DEFAULT VALUES
// ===========================================
class DefaultValues {
byte myByte; // 0
short myShort; // 0
int myInt; // 0
long myLong; // 0L
float myFloat; // 0.0f
double myDouble; // 0.0
char myChar; // '\u0000' (null character)
boolean myBoolean; // false
String myString; // null
void showDefaults() {
System.out.println(myByte); // 0
System.out.println(myBoolean); // false
System.out.println(myString); // null
}
}
// β Local variables have NO default values!
void test() {
int x;
System.out.println(x); // β Compile error! Variable not initialized
}
// ===========================================
// 7. CHOOSING THE RIGHT TYPE
// ===========================================
// β
Use byte for small numbers (-128 to 127)
byte age = 25;
byte temperature = -10;
// β
Use int for most integer calculations (default)
int studentCount = 500;
int yearOfBirth = 1990;
// β
Use long for very large numbers
long worldPopulation = 8_000_000_000L;
long fileSize = 1_500_000_000L;
// β
Use double for decimals (default, more precise than float)
double price = 19.99;
double pi = 3.14159265359;
// β
Use float only when memory is critical (rare)
float gpuCoordinate = 100.5f; // Graphics/gaming
// β
Use boolean for flags
boolean isLoggedIn = true;
boolean hasAccess = false;
// β
Use char for single characters
char grade = 'A';
char symbol = '$';
// β
Use String for text
String username = "alice123";
String message = "Welcome!";5. The Comparison & Decision Layer
| Type | Size | Range | Default | Use Case |
|---|---|---|---|---|
| byte | 8-bit | -128 to 127 | 0 | Small numbers, flags |
| short | 16-bit | -32,768 to 32,767 | 0 | Rare (use int) |
| int | 32-bit | Β±2.1 billion | 0 | Most integers |
| long | 64-bit | Β±9 quintillion | 0L | Very large numbers |
| float | 32-bit | ~7 digits precision | 0.0f | Graphics (rare) |
| double | 64-bit | ~15 digits precision | 0.0 | Most decimals |
| char | 16-bit | 0 to 65,535 (Unicode) | '\u0000' | Single characters |
| boolean | 1-bit | true/false | false | Flags, conditions |
6. The "Interview Corner" (The Edge)
The "Killer" Interview Question: "What's the difference between primitive and non-primitive types?" Answer:
Primitive (8 types):
- Stored directly in stack memory
- Hold actual values
- No methods (can't call .toString())
- Not null (have default values: 0, false, etc.)
- Passed by value (copy)
Non-Primitive (String, Arrays, Objects):
- Stored in heap memory (reference in stack)
- Hold reference to object
- Have methods (can call .length(), .toString())
- Can be null
- Passed by reference (same object)
java
// Primitive
int x = 10; // Value stored directly
int y = x; // Copy of value
y = 20; // x still 10 (different variables)
// Non-primitive
String s1 = "Hello";
String s2 = s1; // Both reference same object
s2 = "World"; // s1 still "Hello" (Strings immutable)Pro-Tips:
- Use underscores for readability:
java
long population = 8_000_000_000L; // β
Clear!
double price = 1_499.99; // β
Easy to read!
int binary = 0b1111_0000; // β
Group bits!- Integer promotion in expressions:
java
byte a = 10;
byte b = 20;
byte c = a + b; // β Compile error!
// Why? a + b promotes to int!
// β
Fix: Cast back to byte
byte c = (byte)(a + b);
// Or use int:
int c = a + b; // β
Works!