{"id":"printing-first-program","slug":"first-program","title":"Your first Java program","text":"A **program** is a list of instructions that a computer carries out one after another. You write the instructions as text, called **source code**. Java can't run that text directly: first a tool called the **compiler** (named `javac`) checks it and translates it into a compact form, and then the **Java virtual machine** runs the result. When you press **Check** on this site, both happen right in your browser, with the real Java 21 compiler.\n\n### The shape of a Java program\n\n```java\npublic class Main {\n    public static void main(String[] args) {\n        System.out.println(\"Hello, Java!\");\n    }\n}\n```\n\n```output\nHello, Java!\n```\n\nAlmost every line here is a frame that you'll write the same way every time:\n\n- `public class Main { ... }` creates a **class** called `Main`. All Java code lives inside a class. The class name has to match the name of the file, and on this site the file is always `Main.java`, so the class is always `Main`.\n- `public static void main(String[] args) { ... }` is the **main method**: the place where the program starts running. The words around `main` all have a meaning that later modules explain. For now, copy the line exactly as it is.\n- `System.out.println(\"Hello, Java!\");` is the one line that does something: it prints the text between the double quotes and then ends the line. The quotes themselves are not printed.\n- Every `{` has a matching `}`. The class's braces wrap the main method, and the main method's braces wrap the instructions.\n\nIn other Java courses and tools the class often has a different name, such as `Sandbox` or `Program`. That's fine: the rule is only that the class name and the file name match.\n\n### Capital letters matter\n\nJava reads `System` and `system` as two different words. `System.out.println` has a capital `S` and nothing else in capitals. If one letter is off, the compiler stops and says what it couldn't understand. Step 5 of this module shows how to read those messages.\n","task":"Fill in the blank so the program prints `Grace Hopper`, the name of a computing pioneer.\n","kind":"fill","seed":"public class Main {\n    public static void main(String[] args) {\n        System.out.[[println]](\"Grace Hopper\");\n    }\n}\n","solution":"public class Main {\n    public static void main(String[] args) {\n        System.out.println(\"Grace Hopper\");\n    }\n}\n","hints":["The method that prints a line is `println`, short for \"print line\".","Type only the method name into the blank: `println`. The dot and the parentheses are already there."],"tests":[{"name":"Output","stdin":"","expect":"Grace Hopper","hidden":false}],"require":[],"forbid":[],"more":[{"task":"Fill in the frame of the program: the word that starts a class, and the two missing words of the main method. The program should print `Every program needs a frame.`\n","kind":"fill","seed":"public [[class]] Main {\n    public [[static]] void [[main]](String[] args) {\n        System.out.println(\"Every program needs a frame.\");\n    }\n}\n","solution":"public class Main {\n    public static void main(String[] args) {\n        System.out.println(\"Every program needs a frame.\");\n    }\n}\n","hints":["The first line of the program creates a class, so the missing word is `class`.","The main method line always reads `public static void main(String[] args)`.","`main` is written in lower case. With a capital `M` the program compiles, but Java can't find where to start."],"tests":[{"name":"Output","stdin":"","expect":"Every program needs a frame.","hidden":false}],"require":[],"forbid":[]},{"task":"This time the editor is empty. Write the whole program yourself: the class, the main method and one line that prints `Ready, set, code!`\n","kind":"code","seed":"// Write the whole program below this line.\n","solution":"// Write the whole program below this line.\npublic class Main {\n    public static void main(String[] args) {\n        System.out.println(\"Ready, set, code!\");\n    }\n}\n","hints":["Start with `public class Main {` and finish with a closing `}`.","Inside the class goes `public static void main(String[] args) {` and its own closing `}`.","Inside main: `System.out.println(\"Ready, set, code!\");` Every quote, parenthesis and the semicolon count."],"tests":[{"name":"Output","stdin":"","expect":"Ready, set, code!","hidden":false}],"require":[],"forbid":[]}]}