Printing and PDF export are disabled for this content. View it online at Full Stack Learning Simplified.
Your First Java Programπ± Beginner
Every Java application lives inside a class and begins execution from a specific main method. Let's write, compile, and run the classic "Hello, World!" program.
What is the Structure?
Java code must be enclosed in a class, and the name of the public class must exactly match the name of the file it is saved in. Inside this class, you define a method called main, which acts as the entry point for the JVM.
How to Write It
Create a new file named Main.java and add the following code:
Main.java
public class Main {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}How to Run It (Terminal)
Historically, running Java was a two-step process: compiling the source code into bytecode, and then executing that bytecode.
bash
javac Main.java # Step 1: Compiles source into Main.class (bytecode)
java Main # Step 2: Runs the bytecode
# Output: Hello, World!Pro Tip: Since Java 11, you can run a single-file program directly in one step using
java Main.java. The compilation happens seamlessly in memory!Breaking Down the Syntax
Let's dissect the code line-by-line so you understand exactly what each keyword does:
| Syntax | Explanation |
|---|---|
public class Main | Defines a public class named Main. This must match the filename Main.java. |
public static void main | The exact signature the JVM looks for to start the program. public means accessible anywhere, static means it belongs to the class itself, and void means it returns no value. |
String[] args | An array of strings used to pass command-line arguments to your program. |
System.out.println(...) | A built-in command that prints text to the console, followed by a new line. |
Troubleshooting Common Compiler Errors:
cannot find symbol: Double-check your spelling and capitalization (Java is strictly case-sensitive).class Main is public, should be declared in a file named Main.java: Rename your file so the filename exactly matches the class name (Main.java).
Free preview. Sign in and subscribe to unlock all 982 lessons across 31 courses.
Free preview Β· Β© 2026 Full Stack Learning Simplified