Ticker

6/recent/ticker-posts

First Java Program — Hello World: How to Create Your First Java Program


First Java Program — Hello World: How to Create Your First Java Program

Introduction

Java is a popular object-oriented programming language used for building a wide variety of applications. Creating a simple "Hello World" program is often the first step for beginners to get started with Java programming. In this documentation, we will guide you through the process of writing and running your first Java program.

Prerequisites

Before proceeding, make sure you have Java Development Kit (JDK) installed on your computer. You can download the latest version of JDK from the Oracle website or adopt OpenJDK.

Step 1: Set Up Your Development Environment

To write and compile Java code, you need a text editor and a Java compiler. Popular text editors include Visual Studio Code, IntelliJ IDEA, or Eclipse. Choose the one that suits your preferences.

Step 2: Write the "Hello World" Program

Now, let's create the "Hello World" program in Java:

java
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}

Explanation of the code:

  • public class HelloWorld: This line declares a public class named "HelloWorld". The class name must match the file name (i.e., the name of the .java file).

  • public static void main(String[] args): This line defines the main method, which serves as the entry point for the Java program. It is mandatory in every Java program. The String[] args is an array of command-line arguments, although it's not used in this simple example.

  • System.out.println("Hello, World!");: This line prints the text "Hello, World!" to the console. The println method is used to display the message and adds a newline after printing.

Step 3: Save the File

Save the Java code in a file named "HelloWorld.java". The file name should match the name used for the class declaration ("HelloWorld" in our example).

Step 4: Compile the Java Program

Open your terminal or command prompt and navigate to the directory where you saved the "HelloWorld.java" file. Compile the code using the Java compiler (javac):

bash
javac HelloWorld.java

If there are no errors in your code, this step will generate a bytecode file named "HelloWorld.class".

Step 5: Run the Java Program

Once the compilation is successful, you can run the Java program using the Java Virtual Machine (JVM):

bash
java HelloWorld

If everything is set up correctly, you should see the output:


Hello, World!

Congratulations! You have successfully created and executed your first Java program!

Conclusion

In this documentation, we learned how to create a simple "Hello World" program in Java. This basic example lays the foundation for building more complex Java applications. Keep exploring Java's features and libraries to unlock its full potential for software development. Happy coding!

Post a Comment

0 Comments