You're building a user profile: age (whole number), height (decimal), first initial (single letter), premium member (yes/no). Each piece of data needs the right container - that's what primitive types are for.

Personal profile data

Store different types of information about a person.

example
Profile.java
Replay: real traced execution (multi-file project)
public class Profile {
    public static void main(String[] args) {
        int age = 25;
        double height = 1.75;
        char initial = 'J';
        boolean isStudent = true;

        System.out.println("Age: " + age);
        System.out.println("Height: " + height + "m");
        System.out.println("Initial: " + initial);
        System.out.println("Student: " + isStudent);
    }
}
public class Profile {
    public static void main(String[] args) {
        int age = 17;
        double height = 1.75;
        char initial = 'J';
        boolean isStudent = true;

        System.out.println("Age: " + age);
        System.out.println("Height: " + height + "m");
        System.out.println("Initial: " + initial);
        System.out.println("Student: " + isStudent);
    }
}
public class Profile {
    public static void main(String[] args) {
        int age = 42;
        double height = 1.75;
        char initial = 'J';
        boolean isStudent = true;

        System.out.println("Age: " + age);
        System.out.println("Height: " + height + "m");
        System.out.println("Initial: " + initial);
        System.out.println("Student: " + isStudent);
    }
}
public class Profile {
    public static void main(String[] args) {
        int age = 25;
        double height = 1.60;
        char initial = 'J';
        boolean isStudent = true;

        System.out.println("Age: " + age);
        System.out.println("Height: " + height + "m");
        System.out.println("Initial: " + initial);
        System.out.println("Student: " + isStudent);
    }
}
public class Profile {
    public static void main(String[] args) {
        int age = 25;
        double height = 1.85;
        char initial = 'J';
        boolean isStudent = true;

        System.out.println("Age: " + age);
        System.out.println("Height: " + height + "m");
        System.out.println("Initial: " + initial);
        System.out.println("Student: " + isStudent);
    }
}
public class Profile {
    public static void main(String[] args) {
        int age = 25;
        double height = 1.75;
        char initial = 'M';
        boolean isStudent = true;

        System.out.println("Age: " + age);
        System.out.println("Height: " + height + "m");
        System.out.println("Initial: " + initial);
        System.out.println("Student: " + isStudent);
    }
}
public class Profile {
    public static void main(String[] args) {
        int age = 25;
        double height = 1.75;
        char initial = 'S';
        boolean isStudent = true;

        System.out.println("Age: " + age);
        System.out.println("Height: " + height + "m");
        System.out.println("Initial: " + initial);
        System.out.println("Student: " + isStudent);
    }
}
public class Profile {
    public static void main(String[] args) {
        int age = 25;
        double height = 1.75;
        char initial = 'J';
        boolean isStudent = false;

        System.out.println("Age: " + age);
        System.out.println("Height: " + height + "m");
        System.out.println("Initial: " + initial);
        System.out.println("Student: " + isStudent);
    }
}
  1. age ← 25, height ← 1.75, initial ← J, isStudent ← true

    1public class Profile {2    public static void main(String[] args) {3        int age→ 25 = 25;           //@age=17, 424        double height→ 1.75 = 1.75;   //@height=1.60, 1.855        char initial→ J = 'J';     //@initial='M', 'S'6        boolean isStudent→ true = true;  //@isStudent=false7        8        System.out.println("Age: " + age25);9        System.out.println("Height: " + height1.75 + "m");10        System.out.println("Initial: " + initialJ);11        System.out.println("Student: " + isStudenttrue);12    }
    outputAge: 25
    Height: 1.75m
    Initial: J
    Student: true
  1. age ← 17, height ← 1.75, initial ← J, isStudent ← true

    1public class Profile {2    public static void main(String[] args) {3        int age→ 17 = 17;4        double height→ 1.75 = 1.75;5        char initial→ J = 'J';6        boolean isStudent→ true = true;7        8        System.out.println("Age: " + age17);9        System.out.println("Height: " + height1.75 + "m");10        System.out.println("Initial: " + initialJ);11        System.out.println("Student: " + isStudenttrue);12    }
    outputAge: 17
    Height: 1.75m
    Initial: J
    Student: true
  1. age ← 42, height ← 1.75, initial ← J, isStudent ← true

    1public class Profile {2    public static void main(String[] args) {3        int age→ 42 = 42;4        double height→ 1.75 = 1.75;5        char initial→ J = 'J';6        boolean isStudent→ true = true;7        8        System.out.println("Age: " + age42);9        System.out.println("Height: " + height1.75 + "m");10        System.out.println("Initial: " + initialJ);11        System.out.println("Student: " + isStudenttrue);12    }
    outputAge: 42
    Height: 1.75m
    Initial: J
    Student: true
  1. age ← 25, height ← 1.6, initial ← J, isStudent ← true

    1public class Profile {2    public static void main(String[] args) {3        int age→ 25 = 25;4        double height→ 1.6 = 1.60;5        char initial→ J = 'J';6        boolean isStudent→ true = true;7        8        System.out.println("Age: " + age25);9        System.out.println("Height: " + height1.6 + "m");10        System.out.println("Initial: " + initialJ);11        System.out.println("Student: " + isStudenttrue);12    }
    outputAge: 25
    Height: 1.6m
    Initial: J
    Student: true
  1. age ← 25, height ← 1.85, initial ← J, isStudent ← true

    1public class Profile {2    public static void main(String[] args) {3        int age→ 25 = 25;4        double height→ 1.85 = 1.85;5        char initial→ J = 'J';6        boolean isStudent→ true = true;7        8        System.out.println("Age: " + age25);9        System.out.println("Height: " + height1.85 + "m");10        System.out.println("Initial: " + initialJ);11        System.out.println("Student: " + isStudenttrue);12    }
    outputAge: 25
    Height: 1.85m
    Initial: J
    Student: true
  1. age ← 25, height ← 1.75, initial ← M, isStudent ← true

    1public class Profile {2    public static void main(String[] args) {3        int age→ 25 = 25;4        double height→ 1.75 = 1.75;5        char initial→ M = 'M';6        boolean isStudent→ true = true;7        8        System.out.println("Age: " + age25);9        System.out.println("Height: " + height1.75 + "m");10        System.out.println("Initial: " + initialM);11        System.out.println("Student: " + isStudenttrue);12    }
    outputAge: 25
    Height: 1.75m
    Initial: M
    Student: true
  1. age ← 25, height ← 1.75, initial ← S, isStudent ← true

    1public class Profile {2    public static void main(String[] args) {3        int age→ 25 = 25;4        double height→ 1.75 = 1.75;5        char initial→ S = 'S';6        boolean isStudent→ true = true;7        8        System.out.println("Age: " + age25);9        System.out.println("Height: " + height1.75 + "m");10        System.out.println("Initial: " + initialS);11        System.out.println("Student: " + isStudenttrue);12    }
    outputAge: 25
    Height: 1.75m
    Initial: S
    Student: true
  1. age ← 25, height ← 1.75, initial ← J, isStudent ← false

    1public class Profile {2    public static void main(String[] args) {3        int age→ 25 = 25;4        double height→ 1.75 = 1.75;5        char initial→ J = 'J';6        boolean isStudent→ false = false;7        8        System.out.println("Age: " + age25);9        System.out.println("Height: " + height1.75 + "m");10        System.out.println("Initial: " + initialJ);11        System.out.println("Student: " + isStudentfalse);12    }
    outputAge: 25
    Height: 1.75m
    Initial: J
    Student: false

Each type serves a purpose: int for count, double for measurement, char for single character, boolean for yes/no.

primitive Built-in types that hold simple values directly (not references to objects).
int 32-bit signed integer: -2,147,483,648 to 2,147,483,647
double 64-bit floating point: ~15 decimal digits of precision
boolean Only two values: `true` or `false`
char Single 16-bit Unicode character: `'A'`, `'7'`, `'@'`

Integer overflow

What happens when a number gets too big for its type?

Overflow.java
Replay: real traced execution (multi-file project)
public class Overflow {
    public static void main(String[] args) {
        int bigNumber = 2000000000;
        int doubled = bigNumber * 2;

        System.out.println("Original: " + bigNumber);
        System.out.println("Doubled:  " + doubled);


    }
}
  1. bigNumber ← 2000000000, doubled ← -294967296

    1public class Overflow {2    public static void main(String[] args) {3        int bigNumber→ 2000000000 = 2000000000;  //#?overflow_start4        int doubled→ -294967296 = bigNumber2000000000 * 2;  //#?overflow_result5        6        System.out.println("Original: " + bigNumber2000000000);7        System.out.println("Doubled:  " + doubled-294967296);
    outputOriginal: 2000000000
    Doubled:  -294967296

int can only hold values up to about 2 billion. Beyond that, it wraps around!

Floating point precision

Decimal numbers aren't always exact. This can surprise you.

Precision.java
Replay: real traced execution (multi-file project)
public class Precision {
    public static void main(String[] args) {
        double a = 0.1;
        double b = 0.2;
        double sum = a + b;

        System.out.println("0.1 + 0.2 = " + sum);
        System.out.println("Equals 0.3? " + (sum == 0.3));

        // For money, use integer cents instead
        int cents1 = 10;
        int cents2 = 20;
        int totalCents = cents1 + cents2;
        System.out.println("10 + 20 cents = " + totalCents);

    }
}
  1. a ← 0.1, b ← 0.2, sum ← 0.30000000000000004, cents1 ← 10, cents2 ← 20

    1public class Precision {2    public static void main(String[] args) {3        double a→ 0.1 = 0.1;4        double b→ 0.2 = 0.2;5        double sum→ 0.30000000000000004 = a0.1 + b0.2;6        7        System.out.println("0.1 + 0.2 = " + sum0.30000000000000004);8        System.out.println("Equals 0.3? " + (sum0.30000000000000004 == 0.3));  //#?precision_issue9        10        // For money, use integer cents instead11        int cents1→ 10 = 10;12        int cents2→ 20 = 20;13        int totalCents→ 30 = cents110 + cents220;14        System.out.println("10 + 20 cents = " + totalCents30);
    output0.1 + 0.2 = 0.30000000000000004
    Equals 0.3? false
    10 + 20 cents = 30

0.1 + 0.2 isn't exactly 0.3 - floating point has tiny rounding errors.

Boolean flags

Booleans represent states and conditions - very useful for tracking status.

example
Booleans.java
Replay: real traced execution (multi-file project)
public class Booleans {
    public static void main(String[] args) {
        boolean isStudent = true;
        boolean hasLicense = false;
        int age = 20;

        boolean isAdult = age >= 18;
        boolean canDrive = isAdult && hasLicense;
        boolean getsDiscount = isStudent || age < 18;

        System.out.println("Adult: " + isAdult);
        System.out.println("Can drive: " + canDrive);
        System.out.println("Gets discount: " + getsDiscount);
    }
}
public class Booleans {
    public static void main(String[] args) {
        boolean isStudent = false;
        boolean hasLicense = false;
        int age = 20;

        boolean isAdult = age >= 18;
        boolean canDrive = isAdult && hasLicense;
        boolean getsDiscount = isStudent || age < 18;

        System.out.println("Adult: " + isAdult);
        System.out.println("Can drive: " + canDrive);
        System.out.println("Gets discount: " + getsDiscount);
    }
}
public class Booleans {
    public static void main(String[] args) {
        boolean isStudent = true;
        boolean hasLicense = true;
        int age = 20;

        boolean isAdult = age >= 18;
        boolean canDrive = isAdult && hasLicense;
        boolean getsDiscount = isStudent || age < 18;

        System.out.println("Adult: " + isAdult);
        System.out.println("Can drive: " + canDrive);
        System.out.println("Gets discount: " + getsDiscount);
    }
}
public class Booleans {
    public static void main(String[] args) {
        boolean isStudent = true;
        boolean hasLicense = false;
        int age = 16;

        boolean isAdult = age >= 18;
        boolean canDrive = isAdult && hasLicense;
        boolean getsDiscount = isStudent || age < 18;

        System.out.println("Adult: " + isAdult);
        System.out.println("Can drive: " + canDrive);
        System.out.println("Gets discount: " + getsDiscount);
    }
}
public class Booleans {
    public static void main(String[] args) {
        boolean isStudent = true;
        boolean hasLicense = false;
        int age = 25;

        boolean isAdult = age >= 18;
        boolean canDrive = isAdult && hasLicense;
        boolean getsDiscount = isStudent || age < 18;

        System.out.println("Adult: " + isAdult);
        System.out.println("Can drive: " + canDrive);
        System.out.println("Gets discount: " + getsDiscount);
    }
}
  1. isStudent ← true, hasLicense ← false, age ← 20, isAdult ← true

    1public class Booleans {2    public static void main(String[] args) {3        boolean isStudent→ true = true;      //@isStudent=false4        boolean hasLicense→ false = false;    //@hasLicense=true5        int age→ 20 = 20;                  //@age=16, 256        7        boolean isAdult→ true = age20 >= 18;8        boolean canDrive→ false = isAdulttrue && hasLicensefalse;9        boolean getsDiscount→ true = isStudenttrue || age20 < 18;10        11        System.out.println("Adult: " + isAdulttrue);12        System.out.println("Can drive: " + canDrivefalse);13        System.out.println("Gets discount: " + getsDiscounttrue);14    }
    outputAdult: true
    Can drive: false
    Gets discount: true
  1. isStudent ← false, hasLicense ← false, age ← 20, isAdult ← true

    1public class Booleans {2    public static void main(String[] args) {3        boolean isStudent→ false = false;4        boolean hasLicense→ false = false;5        int age→ 20 = 20;6        7        boolean isAdult→ true = age20 >= 18;8        boolean canDrive→ false = isAdulttrue && hasLicensefalse;9        boolean getsDiscount→ false = isStudentfalse || age20 < 18;10        11        System.out.println("Adult: " + isAdulttrue);12        System.out.println("Can drive: " + canDrivefalse);13        System.out.println("Gets discount: " + getsDiscountfalse);14    }
    outputAdult: true
    Can drive: false
    Gets discount: false
  1. isStudent ← true, hasLicense ← true, age ← 20, isAdult ← true

    1public class Booleans {2    public static void main(String[] args) {3        boolean isStudent→ true = true;4        boolean hasLicense→ true = true;5        int age→ 20 = 20;6        7        boolean isAdult→ true = age20 >= 18;8        boolean canDrive→ true = isAdulttrue && hasLicensetrue;9        boolean getsDiscount→ true = isStudenttrue || age20 < 18;10        11        System.out.println("Adult: " + isAdulttrue);12        System.out.println("Can drive: " + canDrivetrue);13        System.out.println("Gets discount: " + getsDiscounttrue);14    }
    outputAdult: true
    Can drive: true
    Gets discount: true
  1. isStudent ← true, hasLicense ← false, age ← 16, isAdult ← false

    1public class Booleans {2    public static void main(String[] args) {3        boolean isStudent→ true = true;4        boolean hasLicense→ false = false;5        int age→ 16 = 16;6        7        boolean isAdult→ false = age16 >= 18;8        boolean canDrive→ false = isAdultfalse && hasLicensefalse;9        boolean getsDiscount→ true = isStudenttrue || age16 < 18;10        11        System.out.println("Adult: " + isAdultfalse);12        System.out.println("Can drive: " + canDrivefalse);13        System.out.println("Gets discount: " + getsDiscounttrue);14    }
    outputAdult: false
    Can drive: false
    Gets discount: true
  1. isStudent ← true, hasLicense ← false, age ← 25, isAdult ← true

    1public class Booleans {2    public static void main(String[] args) {3        boolean isStudent→ true = true;4        boolean hasLicense→ false = false;5        int age→ 25 = 25;6        7        boolean isAdult→ true = age25 >= 18;8        boolean canDrive→ false = isAdulttrue && hasLicensefalse;9        boolean getsDiscount→ true = isStudenttrue || age25 < 18;10        11        System.out.println("Adult: " + isAdulttrue);12        System.out.println("Can drive: " + canDrivefalse);13        System.out.println("Gets discount: " + getsDiscounttrue);14    }
    outputAdult: true
    Can drive: false
    Gets discount: true

Use descriptive names: isStudent, hasLicense, isPremiumMember.

Type ranges

Each type has minimum and maximum values it can hold.

Ranges.java
Replay: real traced execution (multi-file project)
public class Ranges {
    public static void main(String[] args) {
        System.out.println("=== Integer Types ===");
        System.out.println("byte:  " + Byte.MIN_VALUE + " to " + Byte.MAX_VALUE);
        System.out.println("short: " + Short.MIN_VALUE + " to " + Short.MAX_VALUE);
        System.out.println("int:   " + Integer.MIN_VALUE + " to " + Integer.MAX_VALUE);
        System.out.println("long:  " + Long.MIN_VALUE + " to " + Long.MAX_VALUE);

        System.out.println("\n=== Floating Types ===");
        System.out.println("float max:  " + Float.MAX_VALUE);
        System.out.println("double max: " + Double.MAX_VALUE);

        System.out.println("\n=== Character ===");
        System.out.println("char: 0 to " + (int)Character.MAX_VALUE);
    }
}
  1. public static void main(String[] args)

    1public class Ranges {2    public static void main(String[] args) {3        System.out.println("=== Integer Types ===");4        System.out.println("byte:  " + Byte.MIN_VALUE + " to " + Byte.MAX_VALUE);5        System.out.println("short: " + Short.MIN_VALUE + " to " + Short.MAX_VALUE);6        System.out.println("int:   " + Integer.MIN_VALUE + " to " + Integer.MAX_VALUE);7        System.out.println("long:  " + Long.MIN_VALUE + " to " + Long.MAX_VALUE);8        9        System.out.println("\n=== Floating Types ===");10        System.out.println("float max:  " + Float.MAX_VALUE);11        System.out.println("double max: " + Double.MAX_VALUE);12        13        System.out.println("\n=== Character ===");14        System.out.println("char: 0 to " + (int)Character.MAX_VALUE);15    }
    output=== Integer Types ===
    byte:  -128 to 127
    short: -32768 to 32767
    int:   -2147483648 to 2147483647
    long:  -9223372036854775808 to 9223372036854775807
    
    === Floating Types ===
    float max:  3.4028235E38
    double max: 1.7976931348623157E308
    
    === Character ===
    char: 0 to 65535

Choose the smallest type that fits your data to save memory in large programs.

Exercise: OtherIntTypes.java

Explore other integer types: byte (tiny), short (small), long (huge)