Hello World in Java

docker pull codearchaeology/java:latest

Every programming journey starts with Hello World. Let’s write our first Java program.

The Code

Create a file named HelloWorld.java:

1
2
3
4
5
public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello, World!");
    }
}

Understanding the Code

  • public class HelloWorld - Defines a public class. In Java, the filename must match the class name.
  • public static void main(String[] args) - The entry point of every Java application.
  • System.out.println() - Prints text to the console followed by a newline.

Running with Docker

The easiest way to run this without installing Java locally:

1
2
3
4
5
6
# Pull the image
docker pull codearchaeology/java:latest

# Run the program
docker run --rm -v $(pwd):/code -w /code codearchaeology/java:latest \
    sh -c "javac HelloWorld.java && java HelloWorld"

Running Locally

If you have Java installed (JDK 11+):

1
2
3
4
5
# Compile
javac HelloWorld.java

# Run
java HelloWorld

Expected Output

Hello, World!

Key Concepts

  1. Java is compiled - Source code (.java) is compiled to bytecode (.class)
  2. Bytecode runs on the JVM - Java Virtual Machine executes the bytecode
  3. Class names matter - The filename must match the public class name
  4. main() is special - This is where program execution begins

Next Steps

Continue to Variables and Data Types to learn about storing and manipulating data in Java.