Chapter 1 · Foundations

Variables and arithmetic

You are reading the whole lesson on one page. Back to one step at a time

Step 1 of 22 · Concept

A variable is a named box

Lesson 2 showed main's frame with boxes in it. A variable is one of those boxes, with a name you choose.

int age = 15;
partmeans
intthe type: what the box can hold
agethe name of the box
= 15the value put in the box

In this lesson you will fill boxes, change them and calculate with them.

public class Area {
    public static void main(String[] args) {
        int width = 7;
        int height = 6;
        int area = width * height;
        System.out.println("Area: " + area);
    }
}
public class Fruit {
    public static void main(String[] args) {
        int apples = 2;
        int pears = 3;
        System.out.println("Fruit: " + apples + pears);
    }
}
public class Birthday {
    public static void main(String[] args) {
        int age = 17;
        boolean adult = age >= 18;
        System.out.println("Age " + age + ", adult: " + adult);

        age = age + 1;
        adult = age >= 18;
        System.out.println("Age " + age + ", adult: " + adult);

        age += 10;
        System.out.println("Ten years later: " + age);
    }
}
public class Receipt {
    public static void main(String[] args) {
        // declare the item, the count and the price

        // print the total and whether delivery is free

        // add 2 to the count, then print both lines again
    }
}
public class Divide {
    public static void main(String[] args) {
        System.out.println(7 / 2);
        System.out.println(7 / 2.0);
        System.out.println(7 % 2);
    }
}
public class Average {
    public static void main(String[] args) {
        int a = 4;
        int b = 7;
        int c = 9;
        double average = (a + b + c) / 3;
        System.out.println("Average: " + average);
    }
}
public class Game {
    public static void main(String[] args) {
        // her name, her points (0 to start), the levels

        // she scores 40 points: print the first line

        // she scores 35 more: print the second line

        // the points per level, and whether she won
    }
}
public class Swap {
    public static void main(String[] args) {
        int a = 3;
        int b = 8;
        // swap the values of a and b
        System.out.println("a = " + a + ", b = " + b);
    }
}
Terminal
Step 1 of 8Width times height

Keyboard shortcuts

The step

ContinueEnter
Previous / next stepAlt+[ · Alt+]
Move along the steps bar← → Home End, then Enter
From a quiz's title to its answers↓
Skip to the stepTab, then Enter (first on the page)
Course outlineAlt+O
These shortcuts?

The workspace

Run the stepCtrl+Enter (⌘↵ on a Mac)
Lesson → editor → consoleF6 (Shift+F6 back)
Output ↔ TerminalCtrl+`
Focus the editorCtrl+. (⌘. on a Mac)
Hide or show the lessonAlt+L
Restore, close a messageEsc (outside the editor and the terminal)

The editor

Leave the editorF6 (Shift+F6 back); or Ctrl+M (⌃⇧M on a Mac), then Tab moves the focus (not in the vim mode)
Open a file by nameCtrl+P (⌘P on a Mac)
All commandsCtrl+Shift+P (⌘⇧P on a Mac) or F1
Go to a lineCtrl+G (⌃G on a Mac)
Show or hide the filesCtrl+B (⌘B on a Mac)
Previous / next file tabCtrl+Shift+[ · Ctrl+Shift+] (⌘⇧[ · ⌘⇧] on a Mac)
The file tabs← → Home End on a tab; Delete closes it
Save the files nowCtrl+S (⌘S on a Mac)
Go to a symbol in the fileCtrl+Shift+O (⌘⇧O on a Mac)
Go to the definitionF12
Format the fileCtrl+Shift+F (⌘⇧F on a Mac), or Shift+Alt+F
Go back / forward after a jumpAlt+← · Alt+→ (⌃- · ⌃⇧- on a Mac)