Java BasicsCore· 30 min read

Your First Program

Every Java program lives inside a class and starts at a special method called main.

What you will learn

  • Write a class with a main method
  • Print text with System.out.println
  • Add comments to explain your code

The classic first program

Let us write the program everyone starts with — it prints a friendly message. Type this into a file named Hello.java:

A complete Java program
// Hello.java — my first Java program
public class Hello {
    public static void main(String[] args) {
        System.out.println("Hello, world!");
    }
}

Note: Output: Hello, world! When you run java Hello, Java looks for main and runs the line inside it. System.out.println(...) prints whatever is in the quotes, then moves to a new line.

What each part means

  • public class Hello — every Java program lives inside a class. The class name (Hello) must match the file name (Hello.java).
  • public static void main(String[] args) — this is the main method, the exact spot where Java starts running. Every runnable program needs it, written just like this.
  • System.out.println(...) — the built-in command to print a line of text.
  • Curly braces { } mark the start and end of a block. The main block sits inside the class block.
  • Semicolon ; ends each statement, like a full stop in a sentence.

Comments: notes for humans

A comment is a note the computer ignores. It is for humans reading the code. Java has two styles:

Two ways to write comments
// This is a single-line comment

/* This is a
   multi-line comment */

System.out.println("Comments are skipped by Java");

Note: Output: Comments are skipped by Java Only the println line produces output. Both comment styles are completely ignored when the program runs — they exist purely to explain your code.

Watch out: Two beginner mistakes to avoid: forgetting the semicolon ; at the end of a statement, and mismatching curly braces { }. Every opening brace needs a matching closing brace.

Tip: System.out.print(...) (without ln) prints WITHOUT moving to a new line. Use println when you want each message on its own line.

Q. Where does a Java program start running?

Answer: Java always begins at the main method — public static void main(String[] args). Code outside main does not run on its own.

✍️ Practice

  1. Change the message to print your own name, then compile and run it.
  2. Add a single-line comment above the println explaining what it does.

🏠 Homework

  1. Write a program that prints three lines: your name, your favourite hobby, and why you want to learn Java.
Want to learn this with a mentor?

CodingClave runs guided, project-based training (28-day, 45-day & 6-month batches).

Explore Training →