Skip to main content

Beginner-Friendly Topic

Take your time - it's perfectly normal to re-read this topic 2-3 times. Try the interactive code editor below to run code yourself. Use the Q&A section to check your understanding before moving on.You've got this!

Lesson 2 of 10

Primitive Types & Wrapper Classes

Java has 8 primitive types - the building blocks of all data. Each has a fixed size, a range, and a corresponding Wrapper class for use with collections and generics. Understanding primitives vs objects is foundational - it affects performance, null handling, and API design.

15 minBy Priygop Editorial TeamUpdated 2026Peer Reviewed & Verified

The 8 Primitive Types

  • byte - 8-bit, range -128 to 127. Use for raw binary data, file I/O.
  • short - 16-bit, range -32,768 to 32,767. Rarely used.
  • int - 32-bit, range ~-2 billion to +2 billion. Default for integers.
  • long - 64-bit. Use for timestamps, large IDs. Suffix: 123L
  • float - 32-bit decimal. Rarely used. Suffix: 3.14f
  • double - 64-bit decimal. Default for decimals. No suffix: 3.14
  • boolean - true or false only. 1-bit logical value.
  • char - 16-bit Unicode character. Single quotes: 'A'

Primitives, Wrappers & Auto-boxing

Primitives, Wrappers & Auto-boxing
public class TypesDemo {
    public static void main(String[] args) {
        // Primitives - stored on the stack, fast
        int age = 25;
        double price = 9.99;
        boolean isActive = true;
        char grade = 'A';
        long bigNumber = 9_000_000_000L;  // underscore for readability (Java 7+)

        // Type casting - widening (automatic)
        int x = 100;
        long y = x;       // int → long: safe, automatic
        double z = x;     // int → double: safe, automatic

        // Narrowing cast - explicit, may lose data
        double pi = 3.14159;
        int truncated = (int) pi;  // 3 - decimal part lost!
        System.out.println("Truncated pi: " + truncated);

        // Wrapper classes - objects that wrap primitives
        Integer boxedAge = 25;         // auto-boxing: int → Integer
        int unboxed = boxedAge;        // auto-unboxing: Integer → int

        // Wrapper utility methods
        int parsed = Integer.parseInt("42");          // String → int
        String str = Integer.toString(42);            // int → String
        int max = Integer.MAX_VALUE;                  // 2147483647
        System.out.println("Max int: " + max);

        // Useful for null checks (primitives CAN'T be null)
        Integer nullableAge = null;  // OK - Integer is an object
        // int nullableInt = null;   // ERROR - primitive can't be null
    }
}

Common Mistakes

  • int / int = int: 7/2 = 3, not 3.5. Cast first: (double) 7 / 2 = 3.5
  • == on Integer compares object references, not values above 127. Use .equals(): age1.equals(age2)
  • float rounding: 0.1f + 0.2f != 0.3f. Use double or BigDecimal for financial math.
  • char is unsigned - it can hold values 0-65535, not negative.
  • long literals MUST have L suffix: 9000000000 without L causes compile error (too big for int)

Quick Quiz

Tip

Tip

Practice Primitive Types Wrapper Classes in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.

Diagram
Loading diagram…

Visual architecture and concepts for Primitive Types & Wrapper Classes

Practice Task

Note

Practice Task - (1) Write a working example of Primitive Types Wrapper Classes from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.

Common Mistake

Warning

A common mistake with Primitive Types Wrapper Classes is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready java code.

Key Takeaways

  • Java has 8 primitive types - the building blocks of all data.
  • byte - 8-bit, range -128 to 127. Use for raw binary data, file I/O.
  • short - 16-bit, range -32,768 to 32,767. Rarely used.
  • int - 32-bit, range ~-2 billion to +2 billion. Default for integers.

Topics in This Module

0/10 done