1.1 Your First Java Program

The classic first program when introducing any new language is Hello World, or a program that prints Hello World to the console. In Java, Hello World can be written as such:

public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello world!");

As compared to other languages like Python, this may seem needlessly verbose. However, there are several reasons for the verbosity of Java, which will be covered in the next few chapters. For now, notice some key syntatical features of the code snippet above:

  • The class declaration public class HelloWorld: in Java, all code lives within classes.

  • The main function: all the code that runs must be inside of a method declared as public static void main(String[] args). Future chapters will cover the exact meaning of this declaration.

  • Curly braces {} enclose sections of code (functions, classes, and other types of code that will be covered in future chapters).

  • All statements must end with a semi-colon.

For fun, see Hello world! in other languages.

Last updated