Your First Java Program
Reading Time: 8 Minutes
Difficulty: Beginner
Topic Summaryโ
Writing your first Java program is the classic starting point for any developer. We write the code in a text file, compile it into machine-readable bytecode using the compiler (javac), and then execute it using the JVM (java).
In this lesson, you will learn how to write the classic "Hello World" program and understand what each line of code actually means.
What You'll Learnโ
- How to write a standard Java program structure
- Line-by-line anatomy of a Java program
- How to compile Java code from the terminal
- How to run Java code
- Common compilation and syntax errors and how to fix them
Prerequisitesโ
- Installing Java (JDK 21) (Lesson 006)
- JDK vs JRE vs JVM (Lesson 004)
Explanationโ
The "Hello World" Codeโ
Create a file named Main.java and type the following code:
public class Main {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
Real-World Analogyโ
Think of a Recipe.
- Writing Code = Writing a recipe in English.
- Compiler (
javac) = Translating the recipe into a universal system code that a robot chef can understand. - JVM (
java) = The robot chef reading the translated instructions and actually baking the cake.
If you make a grammar mistake in the recipe, the translator will stop you and complain before the chef even starts cooking.
Anatomy of the Codeโ
Let's break down each part of our first program:
1. public class Mainโ
public: An access modifier that means this class is accessible from anywhere.class: A keyword used to define a class in Java. Everything in Java must live inside a class.Main: The name of the class. Important: The class name MUST match the file name exactly (case-sensitive). Since our class is namedMain, the file name must beMain.java.
2. public static void main(String[] args)โ
This is the entry point of any Java application. When you run a Java program, the JVM automatically looks for this exact line to start execution.
public: Accessible to the JVM from outside the class.static: Allows the JVM to call this method without creating an instance (object) of the class.void: The return type.voidmeans this method does not return any value.main: The name of the method. This name is reserved for the entry point.String[] args: Parameters passed to the program from the command line (an array of text strings).
3. System.out.println("Hello, World!");โ
This line prints text to the screen.
System: A built-in Java class that contains useful system tools.out: A field in theSystemclass representing the standard output stream (your terminal screen).println: Short for "print line". It prints the text inside the parentheses and then moves the cursor to a new line.;: The semicolon. In Java, every statement must end with a semicolon, just like a sentence ends with a period.
How to Compile and Run Your Programโ
Open your terminal or command prompt and navigate to the folder where you saved Main.java.
Step 1: Compile the Codeโ
The compiler translates your source code (.java) into bytecode (.class). Run the following command:
javac Main.java
If successful, no output will appear, but a new file named Main.class will be created in the same folder.
Step 2: Run the Codeโ
Run the compiled bytecode using the JVM. Run the following command (do not include the .class extension):
java Main
Outputโ
Hello, World!
Common Mistakesโ
- โ Filename mismatch: Naming your file
main.java(lowercase) while the class ispublic class Main(uppercase).- Fix: Rename the file to
Main.java.
- Fix: Rename the file to
- โ Missing semicolon: Leaving out the
;at the end of theprintlnstatement.- Fix: Add
;to the end of the statement.
- Fix: Add
- โ Typo in main method signature: Typing
public static void Main(capital M) or forgettingString[] args.- Fix: Ensure it is exactly
public static void main(String[] args).
- Fix: Ensure it is exactly
- โ Running with .class extension: Executing
java Main.classinstead ofjava Main.- Fix: Run it as
java Main.
- Fix: Run it as
Best Practicesโ
- Always use proper indentation (usually 4 spaces or 1 tab) so your code is readable.
- Use PascalCase for class names (e.g.
MyFirstProgram) and camelCase for method/variable names. - Always match file names exactly with public class names.
Interview Questionsโ
Q1. What is the entry point of a Java program?โ
The entry point of any Java program is the public static void main(String[] args) method. The JVM automatically searches for this method signature to start running the application.
Q2. Why is the main method static?โ
It is marked static so that the JVM can execute it directly without having to instantiate (create an object of) the class first. This saves memory and simplifies startup.
Q3. What is the difference between print() and println()?โ
print() prints the text and leaves the cursor on the same line, so the next printed text will appear immediately after. println() prints the text and moves the cursor to the next line.
Quick Revisionโ
- Every Java program starts with a class definition.
- The file name must match the public class name exactly.
- Java is case-sensitive (
Mainis different frommain). - Use
javacto compile to bytecode, andjavato run using the JVM.
Related Topicsโ
- Java Program Execution Flow (Lesson 005)
- Variables in Java (Lesson 008)
Next Lessonโ
Lesson 008 โ Variables