Part 01 / Topic 4 of 7
Compiling and running: javac and java
Every Java program needs at least one class, and to run it, that class needs a main method with this exact shape:
public static void main(String[] args) {
// your code
}
Don't worry about what each word means yet (static is explained in Stage 2; for now, type it exactly like this). System.out.println(...) prints a line of text.
Real-world example: your first program, saved as Hello.java.
public class Hello {
public static void main(String[] args) {
System.out.println("Hello, World!");
System.out.println("Welcome to Java Step by Step");
}
}
Two separate commands turn this into a running program:
$ javac Hello.java
$ java Hello
Hello, World!
Welcome to Java Step by Step
javac Hello.javacompiles: it readsHello.javaand writesHello.classnext to it. No output means it worked.java Helloruns: note there is no.javaand no.classhere — you givejavathe class name, and it findsHello.classitself.
One hard rule: a public class must live in a file with the exact same name, including capitals: public class Hello must be in Hello.java, not hello.java or Hello.txt.
Full program: examples/Hello.java.
Done here? The practice set for Part 1 — including the solutions — is on the part page.