Now it is time to put everything together and write your first Java program. You have already learned about the JDK, JRE, JVM, environment setup, JAVA_HOME, PATH, Java versions, and development tools. This program will connect those ideas into one simple workflow.
Our first program will be intentionally small. The goal is not to build something complicated. The goal is to understand how Java source code is written, compiled, and executed.
First milestone: If you can create, compile, and run this program successfully, you have completed one of the most important beginner steps in Java.
Creating the Java File
First, create a file named:
Hello.java
The .java extension tells us that this is a Java source file.
Inside the file, write:
public class Hello {
public static void main(String[] args) {
System.out.println("Hello Java");
}
}
At first glance, this code may look strange. Don't worry about memorizing every word yet. We will understand each important part step by step.
Understanding the Class
The first line is:
public class Hello {
Here, class tells Java that we are defining a class.
Hello is the name of the class.
The opening curly brace { marks the beginning of the class body.
There is also an important naming rule here. Because the class is named Hello and it is declared as public, the source file should be named:
Hello.java
Remember: For a public class, the Java source filename must match the public class name, followed by .java.
Understanding the main Method
Inside the class, we have:
public static void main(String[] args) {
This is the main method. It is the standard entry point used to start a basic Java application.
When the Java runtime launches this class, it looks for an appropriate main method to begin execution.
You do not need to master every keyword in this declaration on your first day. For now, understand its most important role:
main() ↓ Starting point of the application
Understanding System.out.println()
The next line is:
System.out.println("Hello Java");
This statement prints text to the console.
The text inside the quotation marks is:
"Hello Java"
Therefore, when the program runs, the output is:
Hello Java
This tiny statement introduces an important programming concept: an instruction that tells the program to perform an action.
Understanding the Complete Program
public class Hello {
public static void main(String[] args) {
System.out.println("Hello Java");
}
}
You can mentally break the program into three levels:
Class ↓ main method ↓ print statement
The class contains the main method, and the main method contains the statement that prints the message.
Compiling the Program
Writing the source code is only the first part. Java source code must be compiled before it can be executed in the normal command-line workflow.
Open Command Prompt in the directory containing Hello.java and run:
javac Hello.java
The javac compiler reads the source code and converts it into Java bytecode.
Hello.java
↓
javac
↓
Hello.class
If there are no compilation errors, you should see a new file named:
Hello.class
This class file contains the bytecode that the JVM can execute.
Running the Program
After successful compilation, run the program using:
java Hello
The Java runtime starts the class and executes its main method.
The output should be:
Hello Java
Notice something important: when running the class, you write Hello, not Hello.class.
Remember: Compile with javac Hello.java. Run with java Hello.
The Complete Workflow
Your first Java program follows the same fundamental workflow that larger Java applications use.
Hello.java
↓
Java Source Code
↓
javac Hello.java
↓
Hello.class
↓
Java Bytecode
↓
java Hello
↓
JVM
↓
Hello Java
This is the practical connection between the concepts you learned earlier about the JDK, compiler, bytecode, and JVM.
What Happens Inside the JVM?
When you execute:
java Hello
the Java runtime starts the class and the JVM takes responsibility for executing the bytecode.
The simplified flow is:
Hello.class
↓
Class Loading
↓
Bytecode Verification
↓
JVM Execution
↓
Program Output
You do not need to understand every internal JVM component yet. At this stage, the important concept is that the JVM executes the bytecode produced from your Java source code.
What If There Is a Compilation Error?
Suppose you accidentally forget a semicolon:
System.out.println("Hello Java")
When you compile the program, the compiler reports an error instead of successfully producing the expected class file.
This demonstrates an important relationship:
Source Code
↓
Compiler
↓
Error?
┌──┴──┐
Yes No
↓ ↓
Fix .class
Code ↓
Run
The compiler is therefore your first line of defense against many syntax errors.
What If the Program Runs but Prints the Wrong Output?
Compilation success does not guarantee that the program does exactly what you intended.
For example:
System.out.println("Good Morning");
This code is syntactically valid, but if you expected Hello Java, the program still does not meet your expectation.
This introduces an important distinction:
| Situation | Meaning |
|---|---|
| Compilation error | The compiler found a problem with the source code. |
| Program runs successfully | The code compiled and the application started. |
| Unexpected output | The program runs, but its behavior is not what you intended. |
Common Beginner Mistakes
- Naming the file differently from the public class name.
- Forgetting the main method.
- Forgetting semicolons at the end of statements.
- Using java Hello.java when following the traditional compile-then-run workflow with javac and java.
- Running java Hello.class instead of java Hello.
- Trying to run the program before successfully compiling it.
- Ignoring the exact error message produced by the compiler.
A Small Experiment
Once your first program works, change the message:
System.out.println("I am learning Java");
Compile it again:
javac Hello.java
Then run it:
java Hello
You should now see:
I am learning Java
This tiny experiment is valuable because you are no longer just copying a program. You are changing the source code, compiling it, and observing the new result.
Instructor tip: Do small experiments immediately after learning a concept. Changing one line and observing what happens builds stronger understanding than simply reading another example.
Small Learning Checkpoint
Try explaining this sequence in your own words:
Hello.java
↓
javac Hello.java
↓
Hello.class
↓
java Hello
↓
Program Output
If you can explain why each step exists, you already understand the basic Java development workflow.
Interview Insight
A common beginner interview question is: "What happens when you compile and run a Java program?"
A strong answer is: "The Java source file is compiled by javac into bytecode stored in a class file. When the program is launched, the JVM loads and executes that bytecode."
Another common question is: "Why should the filename match the public class name?"
For a public top-level class, Java requires the source filename to match the public class name. Therefore, public class Hello belongs in Hello.java.
Quick Revision
| Step | What Happens |
|---|---|
| Write | Create Java source code in a .java file. |
| Compile | Use javac to convert source code into bytecode. |
| Class File | The compiled bytecode is stored in a .class file. |
| Run | Use the java command to launch the application. |
| JVM | Executes the Java bytecode. |
| Output | The program produces its result through statements such as System.out.println(). |
Your first Java program may contain only a few lines, but it represents the complete foundation of Java development: source code is written, the JDK compiler converts it into bytecode, and the JVM executes that bytecode. From this point forward, Java becomes much more interesting because you can start replacing the simple message with variables, calculations, conditions, loops, methods, objects, and eventually complete applications.
