Java Hello World Program: A Guide to Writing and Running Your First Code
🧠 What Will You Learn in This Guide?
The most traditional way to start learning Java is with the “Hello World” program.
In this guide, you will learn the basic syntax of Java, write your first Java code and run it via the Command Line (Terminal).
You will also understand step by step how to use the javac and java commands, the compilation process, and how the JVM works.
☕ Stage 1: Basic Structure of the Java Program
All Java programs are located in a class**.
The point where the application starts working is the main() method.
🧩 Code Example
public class JavaHelloWorldProgram {
public static void main(String[] args) {
System.out.println("Merhaba Dünya");
}
}
💬 Description:
public class JavaHelloWorldProgram → It is the class name of the program.
public static void main(String[] args) → JVM starts the program from this method.
System.out.println() → Prints text to the screen.
💾 Step 2: Saving Process
After writing the code, save the file with the same name as the class name.
📂 JavaHelloWorldProgram.java
💡 Why is it important?
In Java, the public class name and file name must be exactly the same. Otherwise, a compilation error will be received (class JavaHelloWorldProgram is public, should be declared in a file named JavaHelloWorldProgram.java).
⚙️ Step 3: Compiling and Running
Java programs are converted into intermediate code called bytecode before being run.
1️⃣ Compile
Open the command line (CMD or Terminal) and navigate to the directory where the file is located:
javac JavaHelloWorldProgram.java
💬 Description: This command compiles the source code and creates a bytecode file named JavaHelloWorldProgram.class.
2️⃣ Run
java JavaHelloWorldProgram
💬 Description: This command runs the compiled class on the JVM and prints the result to the console.
🖥️ Sample Output:
Merhaba Dünya
💡 Additional Information: In Java 11 and above, compiling and running can be done with a single command:
java JavaHelloWorldProgram.java
This command automatically compiles and runs the code.
🔍 Working Logic of Java Programs
The source file with .java extension is written.
The javac compiler converts this file to bytecode with .class extension.
java command runs bytecode on JVM (Java Virtual Machine).
JVM finds the main() method and starts execution.
💡 Summary:
Source Code → Compilation → Bytecode → JVM → Running Program
⚙️ Important Points About Java Hello World Program
| Rule | Description |
|---|---|
1️⃣ Each .java file can only contain one public class. | However, there may be more than one non-public class. |
2️⃣ The file name must be the same as the name public class. | Ex: public class Test → Test.java |
3️⃣ Compilation results in bytecode with .class extension. | JVM runs this file. |
| 4️⃣ The file extension is not written during execution. | java JavaHelloWorldProgram ✔️ |
5️⃣ The JVM initializes the program from the main() method. | The signature of this method must be complete. |
📚 Frequently Asked Questions (FAQ)
- Why should the source file name be the same as the class name?
The Java compiler (javac) derives the public class name from the file name. Otherwise, compilation will fail.
- What is the difference between javac and java commands?
javac converts source code into bytecode. Java runs bytecode in the JVM.
- Why is the full signature of the main() method mandatory?
JVM looks for the method with the signature public static void main(String[] args). If written differently, it gives an error: Error: Main method not found in class...
- What is Bytecode and why is it used?
Bytecode is platform-independent intermediate code. In this way, Java implements the "Write Once, Run Anywhere" philosophy.
- Do I have to use IDE (Eclipse, IntelliJ)?
No. Working with the command line at startup helps you understand compilation logic. However, IDEs make this process easier.
🧩 Result
Congratulations! 🎉 You have now written, compiled and run your first Java program. This simple example is the first step in understanding how Java works.
☕ Try your own Java project on the GenixNode platform now to speed up your Java learning and test your applications.

