Java is a popular and widely used programming language known for its versatility and platform independence. Whether you’re a beginner programmer or looking to expand your coding skills, creating your first Java program is an essential milestone. In this step-by-step guide, we will walk you through the process of writing and running your first Java program.
Install Java Development Kit (JDK)
To start coding in Java, you need to install the Java Development Kit (JDK) on your computer. The JDK provides the necessary tools and libraries to compile and run Java programs. You can download the latest JDK version from the official Oracle website and follow the installation instructions specific to your operating system.
Set Up the Development Environment
After installing the JDK, it’s time to set up your development environment. You can use any Integrated Development Environment (IDE) of your choice, such as Eclipse, IntelliJ IDEA, or NetBeans. These IDEs provide a user-friendly interface and useful features that streamline the coding process. Install your preferred IDE and configure it to use the installed JDK.
Create a New Java Project
Once your development environment is set up, open your IDE and create a new Java project. Give it a name and specify the location where you want to save it. The IDE will create the necessary folder structure for your project.
Create a Java Class
In Java, programs are built using classes. A class is a blueprint for creating objects that have specific attributes (variables) and behaviors (methods). Within your project, create a new Java class by right-clicking on the project folder, selecting “New,” and then choosing “Class” from the context menu. Give your class a meaningful name, such as “Main,” and click “OK.”
Write the Java Code
Once your class is created, the IDE will open a new editor window for you to write your Java code. In the editor, you will see a template with the class declaration. Write your code within the class body. For example, let’s write a simple program that displays “Hello, World!” on the console:
public class Main {
public static void main(String[] args) {
// Print "Hello, World!" to the console
System.out.println("Hello, World!");
}
}
In this code, we define a class named “Main” with a main method. The main method is the entry point of a Java program, and it is where the program execution begins. Inside the main method, we use the System.out.println() statement to print the string “Hello, World!” to the console.
Save and Compile the Code
After writing the code, save the file with a .java extension. The file name should match the class name, i.e., MyFirstProgram.java. Once saved, you can compile the Java code by clicking on the build or compile button in your IDE, or by using the command line.
Run the Java Program
Once the code is successfully compiled, it’s time to run your program. In your IDE, find the run or execute button and click on it. Alternatively, you can navigate to the directory where your Java file is saved using the command prompt or terminal, and run the program using the java command followed by the class name:
// Compiling the Java program
javac Main.java
// Running the compiled Java program
java Main
You have just executed your first Java program. The output “Hello, World!” should appear in the console.
Conclusion
In this step-by-step guide, we covered the essential steps to create and run your first Java program. Remember to have the Java Development Kit (JDK) installed, set up your development environment with an IDE, create a Java project and class, write the code, compile it, and finally run your program. With this foundation, you can now explore the vast capabilities of Java and continue your journey in learning and building Java applications.
If you wish to learn more about Java, please subscribe to our newsletter today and continue your Java learning journey with us!