You're tracking your monthly budget. Instead of typing 2500.00 every time you calculate savings, rent, and groceries, you store it in a variable called income. Now when your income changes, you update one number and all calculations adjust.

Store and use a value

Give a value a name, then use that name in calculations.

Basic.java
public class Basic {
    public static void main(String[] args) {
        int x = ;
        int y = x + 5;
        int z = x * x;
    }
}
public class Basic {
    public static void main(String[] args) {
        int x = ;
        int y = x + 5;
        int z = x * x;
    }
}
public class Basic {
    public static void main(String[] args) {
        int x = ;
        int y = x + 5;
        int z = x * x;
    }
}

A variable holds a value. Use it anywhere you'd use the value itself.

variable A named container for a value. Declare with type, then assign: `int x = 5;`

Building expressions step by step

Break a complex calculation into named pieces. Each piece has meaning.

Steps.java
public class Steps {
    public static void main(String[] args) {
        int width = ;
        int height = ;
        int area = width * height;
        int perimeter = 2 * (width + height);
    }
}
public class Steps {
    public static void main(String[] args) {
        int width = ;
        int height = ;
        int area = width * height;
        int perimeter = 2 * (width + height);
    }
}
public class Steps {
    public static void main(String[] args) {
        int width = ;
        int height = ;
        int area = width * height;
        int perimeter = 2 * (width + height);
    }
}
public class Steps {
    public static void main(String[] args) {
        int width = ;
        int height = ;
        int area = width * height;
        int perimeter = 2 * (width + height);
    }
}
public class Steps {
    public static void main(String[] args) {
        int width = ;
        int height = ;
        int area = width * height;
        int perimeter = 2 * (width + height);
    }
}
public class Steps {
    public static void main(String[] args) {
        int width = ;
        int height = ;
        int area = width * height;
        int perimeter = 2 * (width + height);
    }
}
public class Steps {
    public static void main(String[] args) {
        int width = ;
        int height = ;
        int area = width * height;
        int perimeter = 2 * (width + height);
    }
}
public class Steps {
    public static void main(String[] args) {
        int width = ;
        int height = ;
        int area = width * height;
        int perimeter = 2 * (width + height);
    }
}
public class Steps {
    public static void main(String[] args) {
        int width = ;
        int height = ;
        int area = width * height;
        int perimeter = 2 * (width + height);
    }
}

Reading area tells you more than reading 7 * 5. Variables make code self-documenting.

int `int` holds whole numbers: -2, 0, 42, 1000. Range: about ±2 billion.

Temperature conversion (Fahrenheit to Celsius)

Convert 98.6°F (body temperature) to Celsius using the formula: C = (F - 32) × 5 / 9

Temperature.java
public class Temperature {
    public static void main(String[] args) {
        double fahrenheit = ;
        double celsius = (fahrenheit - 32) * 5 / 9;
    }
}
public class Temperature {
    public static void main(String[] args) {
        double fahrenheit = ;
        double celsius = (fahrenheit - 32) * 5 / 9;
    }
}
public class Temperature {
    public static void main(String[] args) {
        double fahrenheit = ;
        double celsius = (fahrenheit - 32) * 5 / 9;
    }
}
public class Temperature {
    public static void main(String[] args) {
        double fahrenheit = ;
        double celsius = (fahrenheit - 32) * 5 / 9;
    }
}

Variables make the formula readable: fahrenheit, celsius - you know what they mean.

double `double` holds decimal numbers: 3.14, -0.5, 98.6. Use for measurements.

Fibonacci sequence (without loops)

Each Fibonacci number is the sum of the two before it: 0, 1, 1, 2, 3, 5, 8...

Fibonacci.java
public class Fibonacci {
    public static void main(String[] args) {
        int c0 = 0;
        int c1 = 1;
        int c2 = c0 + c1;
        int c3 = c1 + c2;
        int c4 = c2 + c3;
        int c5 = c3 + c4;
        int c6 = c4 + c5;
        int c7 = c5 + c6;
    }
}

Watch how each variable depends on the previous ones. This pattern is called building up from base cases.

Circle calculations

Given a radius, calculate circumference and area.

Circle.java
public class Circle {
    public static void main(String[] args) {
        double pi = 3.14159;
        double radius = ;
        
        double circumference = 2 * pi * radius;
        double area = pi * radius * radius;
    }
}
public class Circle {
    public static void main(String[] args) {
        double pi = 3.14159;
        double radius = ;
        
        double circumference = 2 * pi * radius;
        double area = pi * radius * radius;
    }
}
public class Circle {
    public static void main(String[] args) {
        double pi = 3.14159;
        double radius = ;
        
        double circumference = 2 * pi * radius;
        double area = pi * radius * radius;
    }
}
public class Circle {
    public static void main(String[] args) {
        double pi = 3.14159;
        double radius = ;
        
        double circumference = 2 * pi * radius;
        double area = pi * radius * radius;
    }
}

pi and radius as named values make the formulas clear.