Hello, Java
Every Java program starts as text in a file. The text in the editor is a complete program: a class named
Hello, with a method named main. When you run a Java program, the computer starts at main and
runs its statements from top to bottom. This one has a single statement, which prints a line.
Press Run, or press ⌘↵ in the editor (Ctrl+Enter on Windows and Linux): it runs the code you see.
public class Hello {
public static void main(String[] args) {
System.out.println("Hello, world!");
}
}
What Run did
The first lines of the output are the two commands Run typed for you:
javacis the Java compiler. It readsHello.java, checks it, and writesHello.class, the compiled program, into the folderout.javais the Java virtual machine. It loadsHello.classand callsmain.
These are the real javac and java of OpenJDK, running in a small Linux computer inside this browser
tab. To make Run quick, the compiler is kept warm in the background, ready for your next program.
The file name and the class name must match: public class Hello lives in Hello.java. Java is
case-sensitive, so hello and Hello are different names.
println and print
System.out.println(…) prints its text and then ends the line. System.out.print(…) prints without
ending the line, so the next output continues on the same line.
Change the program so that it prints exactly these two lines:
Hello, Java!
I am learning.
public class Hello {
public static void main(String[] args) {
System.out.println("Hello, world!");
}
}
Your code is saved in this browser: come back later and it is still here. Terminal, next to
Output, is a shell in the same computer; your files are in /home/student. Alt+[ and Alt+] move between
the steps of a lesson, and Keyboard shortcuts in the ⋯ menu next to Run lists the others.