Lesson Completion
Back to course

Data Types: Primitive and Non-Primitive Types

Beginner
10 minutesβ˜…4.7JavaPlay to Learn

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

TypeSizeRangeDefaultUse Case
byte8-bit-128 to 1270Small numbers, flags
short16-bit-32,768 to 32,7670Rare (use int)
int32-bitΒ±2.1 billion0Most integers
long64-bitΒ±9 quintillion0LVery large numbers
float32-bit~7 digits precision0.0fGraphics (rare)
double64-bit~15 digits precision0.0Most decimals
char16-bit0 to 65,535 (Unicode)'\u0000'Single characters
boolean1-bittrue/falsefalseFlags, 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:

  1. 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!
  1. 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!

Topics Covered

Java FundamentalsJava Syntax

Tags

#java#basics#syntax#variables#data-types#beginner-friendly

Last Updated

2025-02-01