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:
- Java finds the
HelloWorldclass. - The Java launcher looks for the appropriate
mainmethod. - It starts execution from
main. - The program reaches
System.out.println(...). "Hello, World!"is printed.- The program finishes.
🎉 You have just written a Java program!