...
Sign in

Your First Java Program

Write your first Java program and understand every important part of the classic Hello World example.

Share:

Congratulations! 🎉 You are about to write your first Java program.

We will use the traditional Hello World program because it is small enough to understand completely.

Your First Program

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

When this program runs, the output is: Hello, World!

At first glance, this may look complicated. Don't worry. We are going to take it apart piece by piece.

Understanding the Structure

There are several important concepts here: public, class, HelloWorld, static, void, main, String[], args, System, out, println, { }, ( ), ;

Let's understand them individually.

01. public

public is an access modifier. It means that the thing marked public can be accessed from outside its immediate scope, subject to Java's access rules.

For the main method, making it public allows the Java runtime to access the method as the program's entry point. Think of a building. If the entrance is publicly accessible, someone coming from outside can reach it.

02. class

class tells Java: "I am defining a class." A class is a blueprint or structure that can contain data and behavior. For beginners, think of a class as a container that organizes related code.

03. HelloWorld

This is not a Java keyword. It is the name we chose for our class. Java identifiers are names that we give to things such as Classes, Variables, and Methods. By convention, Java class names usually use PascalCase.

04. static

static means that the method belongs to the class itself, rather than requiring an object of that class to be created first. Because main is static, Java can invoke it through the class. Think of it like a reception desk attached directly to the building.

05. void

void tells Java: "This method does not return a value." Some methods calculate something and give a result back, but our main method doesn't return a value to the Java launcher.

06. main

main is the name of the method that serves as the conventional entry point for a standalone Java application. When the Java launcher starts a traditional Java application, it looks for an appropriate main method. Think of main() as the front door of your program.

07. String[]

String represents text. The square brackets [] mean an array. An array is a collection of multiple values of the same type. So String[] means: "An array of String values."

08. args

args is the name of the parameter. It is short for "arguments." It gives your program a place to receive command-line arguments.

What About System.out.println()?

Our program also contains:

System.out.println("Hello, World!");

For now, understand it as: Display this text on the console. The string "Hello, World!" is the message we want to display. The semicolon ; marks the end of the statement.

The Complete Mental Model

When Java launches this program:

  1. Java finds the HelloWorld class.
  2. The Java launcher looks for the appropriate main method.
  3. It starts execution from main.
  4. The program reaches System.out.println(...).
  5. "Hello, World!" is printed.
  6. The program finishes.

🎉 You have just written a Java program!


CheckpointCheckpoint
Question 1 of 3Score: 0/3

Why must the main method in Java be declared as 'static'?

Lesson Milestone

Finished Reading This Lesson?

Mark this lesson complete. A chapter counts as done once all its lessons are, and that adds to your course progress.

TH

Written by

ThinkWisely Team

Have a Question or Suggestion?

We'd love to hear from you. Drop your thoughts below to help us improve ThinkWisely!