Writing Your First Java Program
Writing and running your first Java program is an exciting step toward becoming a Java developer. In this guide, we’ll walk through how to create a simple Hello, World! program using a Java Integrated Development Environment (IDE), which provides all the tools you need to write, compile, and run your code.
Step 1: Install Java and Set Up Your IDE
Before you can start coding, you need to have the Java Development Kit (JDK) and a Java IDE installed on your computer.
Install the JDK:
- Download the JDK from the Oracle website.
- Follow the installation instructions for your operating system (Windows, macOS, or Linux).
Install a Java IDE:
- You can use any Java IDE (such as IntelliJ IDEA, NetBeans, or others).
- Download and install an IDE that suits you. Most Java IDEs will have built-in features like syntax highlighting, debugging tools, and project management that make writing Java code easier.
Step 2: Create a New Java Project
- Open Your IDE and create a new Java project:
- In most IDEs, this can be done by selecting File > New Project and then choosing Java as the project type.
- Give your project a name, such as
HelloWorldProject
.
Step 3: Create a Java Class
Create a New Class:
- In your newly created project, right-click the src folder (or the appropriate folder for your code) and choose New > Class.
- Name the class
HelloWorld
. - Ensure the IDE generates the
main
method for you by checking the box labeled public static void main(String[] args) (this is the entry point of your program).
Write the Code: Replace the code in the
HelloWorld.java
file with the following:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
Explanation:
- public class HelloWorld: This declares a class named
HelloWorld
. All Java programs are built around classes. - public static void main(String[] args): The
main
method is where your program begins execution. It is the entry point for any Java program. - System.out.println(“Hello, World!”);: This line prints the message “Hello, World!” to the console.
Step 4: Run the Program
Run the Program:
- Click the Run button in your IDE (usually a green play icon).
- Alternatively, you can right-click on the
HelloWorld.java
file and select Run.
View the Output: After running the program, you should see the following output in your Console window:
Hello, World!
Conclusion
Congratulations! You’ve successfully written, compiled, and run your first Java program. This simple Hello, World! program introduced you to key Java concepts such as classes, the main
method, and printing output to the console. With your Java IDE set up and ready to go, you’re now equipped to explore more advanced Java programming concepts and start building your own projects.