Writing and Running Your First Java Program: A Beginner’s Tutorial
Last Updated on: 23rd Aug 2025 02:20:47 AM
Welcome to this beginner-friendly tutorial designed for students new to Java programming! In this guide, we’ll walk you through writing and running your first Java program, focusing on creating the classic Hello World program, how to compile and run a Java program, and how a Java program works internally. Each section is explained in a simple, step-by-step way with clear examples and interactive practice ideas to make learning engaging and fun. This tutorial assumes you have the Java Development Kit (JDK) installed and an Integrated Development Environment (IDE) like IntelliJ IDEA, Eclipse, or Visual Studio Code set up. If not, please refer to a Java installation tutorial first. Let’s get started!
1. The Hello World Program
The Hello World program is a simple, classic program that prints “Hello, World!” to the console. It’s the perfect starting point to learn Java’s structure and execution process.
Code for Hello World
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
Explanation of the Code
-
public class HelloWorld: Defines a class named HelloWorld. In Java, all code must be inside a class, and the class name must match the file name (HelloWorld.java).
-
public static void main(String[] args): This is the main method, the entry point where the Java Virtual Machine (JVM) starts running your program.
-
public: The method is accessible from anywhere.
-
static: The method can be called without creating an object of the class.
-
void: The method doesn’t return any value.
-
String[] args: Allows the program to accept command-line arguments (not used here).
-
-
System.out.println("Hello, World!"): Prints “Hello, World!” to the console and adds a new line.
Key Points
-
The file must be saved as HelloWorld.java (matching the class name exactly, as Java is case-sensitive).
-
The main method is required for the program to run.
Interactive Practice
Try It: Save the above code in a file named HelloWorld.java. Then, modify it to print “I’m learning Java!” instead of “Hello, World!”. Save it and prepare to compile and run it in the next sections.
2. How to Compile a Java Program
Compiling converts your Java code (written in a .java file) into bytecode (a .class file) that the JVM can understand. The javac command, part of the JDK, is used for this.
Steps to Compile
-
Write and Save the Program:
-
Save the HelloWorld.java code in a folder, e.g., C:\JavaProjects (Windows) or ~/JavaProjects (Linux/macOS).
-
-
Open a Terminal:
-
Windows: Open Command Prompt by searching cmd in the Start menu.
-
Linux/macOS: Open Terminal (available in your applications).
-
-
Navigate to the Folder:
-
Use the cd command to go to your folder. Example:
-
cd C:\JavaProjects
Compile the Program:
javac HelloWorld.java
-
This creates a HelloWorld.class file in the same folder, which contains the bytecode.
-
If there are errors, check for:
-
File name mismatch: Ensure the file name matches the class name (HelloWorld.java for public class HelloWorld).
-
Syntax errors: Look for missing semicolons (;) or curly braces ({}).
-
JDK setup: Verify the JDK is installed by running javac -version. If not recognized, ensure JAVA_HOME is set and the JDK’s bin folder is in your system PATH.
-
What Happens During Compilation?
-
The javac compiler checks your code for syntax errors (e.g., missing semicolons or incorrect keywords).
-
If the code is valid, it translates it into bytecode, a platform-independent format that the JVM can execute on any device.
3. How to Run a Java Program
Running a Java program executes the compiled bytecode using the java command. The JVM interprets or compiles the bytecode into machine code specific to your device.
Steps to Run
-
Ensure Compilation:
-
Confirm that you’ve compiled HelloWorld.java and have a HelloWorld.class file in your folder.
-
-
Run the Program:
-
In the same terminal and folder, run:
-
java HelloWorld
Note: Do not include the .class extension in the command.
Output:
Hello, World!
4. Compiling and Running in an IDE
Using an Integrated Development Environment (IDE) like IntelliJ IDEA, Eclipse, or Visual Studio Code simplifies the process of compiling and running Java programs. The IDE handles compilation and execution automatically, making it beginner-friendly.
Steps in an IDE
-
Create a New Project:
-
IntelliJ IDEA: Go to File > New > Project, select Java, name the project (e.g., FirstJavaProject), and click Create.
-
Eclipse: Go to File > New > Java Project, name it, and click Finish.
-
VS Code: Open a folder (e.g., JavaProjects), and create a new file named HelloWorld.java. Ensure the Java Extension Pack is installed.
-
-
Write the Hello World Code:
-
Copy the HelloWorld.java code from above into a new file in your project.
-
-
Compile and Run:
-
IntelliJ IDEA: Click the green Run button (triangle) next to the main method or select Run > Run 'HelloWorld'.
-
Eclipse: Right-click the file in the Package Explorer, select Run As > Java Application.
-
VS Code: Click the Run button above the main method or select Run Java from the command palette.
-
The IDE compiles the code into bytecode and runs it, displaying “Hello, World!” in the console window.
-
-
View Output:
-
The output appears in the IDE’s console (usually at the bottom of the window).
-
5. How a Java Program Works Internally
Understanding how a Java program works behind the scenes helps you appreciate its platform independence and execution process. Here’s a simple explanation of the internal workflow:
Step-by-Step Internal Process
-
Writing the Code:
-
You write Java code in a .java file (e.g., HelloWorld.java) using a text editor or IDE.
-
This code is human-readable and follows Java’s syntax rules.
-
-
Compilation (javac):
-
The Java compiler (javac) translates the .java file into bytecode, stored in a .class file (e.g., HelloWorld.class).
-
Bytecode is a platform-independent format, meaning it’s the same regardless of whether you’re using Windows, Linux, or macOS.
-
The compiler checks for syntax errors (e.g., missing semicolons or incorrect keywords) and generates bytecode if the code is valid.
-
-
Java Virtual Machine (JVM):
-
The JVM is a virtual machine that runs on your device and executes the bytecode.
-
The JVM performs several tasks:
-
Class Loader: Loads the .class file and any required Java libraries into memory.
-
Bytecode Verifier: Checks the bytecode for security issues or errors (e.g., invalid operations).
-
Interpreter: Executes the bytecode line by line.
-
Just-In-Time (JIT) Compiler: Converts bytecode into machine code (specific to your device’s hardware and OS) for faster execution.
-
Garbage Collector: Automatically frees memory by removing unused objects, preventing memory leaks.
-
-
The JVM ensures that the same bytecode runs on any device with a JVM installed, making Java platform-independent.
-
-
Execution:
-
The JVM starts by executing the main method in your program.
-
For example, in HelloWorld.java, the JVM processes System.out.println("Hello, World!") by sending the text to your device’s console through the operating system.
-
Diagram of the Process
Java Code (.java) → javac → Bytecode (.class) → JVM (Class Loader → Verifier → Interpreter/JIT Compiler) → Machine Code → Output