Writing and Running Your First Java Program
diagram for Writing and Running Your First Java Program
Java is one of the most popular programming languages in the world. It's known for its portability, object-oriented structure, and wide usage in enterprise applications. If you're just getting started, writing your first Java program is a great step toward becoming a developer.
This guide will walk you through installing Java, writing a simple program, and running it.
Before you can write Java code, you need the Java Development Kit (JDK) installed.
Go to https://www.oracle.com/java/technologies/javase-downloads.html
Download and install the latest JDK version for your operating system.
Verify installation:
java -version
javac -version
You can use any text editor or an Integrated Development Environment (IDE) like:
VS Code
IntelliJ IDEA
Eclipse
For beginners, VS Code or IntelliJ IDEA is recommended.
Open your editor and create a file named HelloWorld.java
. Then, type the following code:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
public class HelloWorld
: Defines a class named HelloWorld.
main(String[] args)
: The entry point of any Java application.
System.out.println
: Prints text to the console.
Use the terminal or command prompt to compile the file:
javac HelloWorld.java
If there are no errors, it creates a HelloWorld.class
file (bytecode).
Execute the program with:
java HelloWorld
Hello, World!
File name and class name should match.
Java is case-sensitive.
Don't forget the semicolon at the end of statements.
Now that you've written your first Java program, explore:
Variables and data types
Control flow (if, switch, loops)
Object-Oriented Programming (OOP) basics
Java packages and modules
Writing and running your first Java program is the foundation of your programming journey. Java is a robust, scalable language that powers everything from Android apps to enterprise systems.
Keep practicing, and soon you'll be building more advanced applications with confidence!