Getting Started with Java: Your First Program

Introduction

link to this section

Java is a powerful and widely-used programming language known for its simplicity, portability, and versatility. In this tutorial, we'll guide you through creating and running your first Java program, commonly known as the "Hello, World!" program. By the end of this tutorial, you'll have a solid understanding of how to set up your Java development environment and write and execute basic Java code.

Creating Your First Java Program

link to this section

Let's create a simple Java program that prints "Hello, World!" to the console:

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

Understanding the Program

link to this section
  • public class HelloWorld { ... } : Defines a class named HelloWorld . In Java, every application must have at least one class with a main method.

  • public static void main(String[] args) { ... } : The main method serves as the entry point for the Java program. It is where the program starts execution.

  • System.out.println("Hello, World!"); : This line prints the string "Hello, World!" to the console. The System.out.println method is used to output text to the standard output stream.

Compiling and Running the Program

link to this section
  1. Save the File : Save the above Java code in a file named HelloWorld.java .

  2. Compile the Program : Open a terminal or command prompt, navigate to the directory containing HelloWorld.java , and run the following command to compile the program:

    javac HelloWorld.java 

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

  3. Run the Program : After successful compilation, run the following command to execute the program:

    java HelloWorld 

    You should see the output:

    Hello, World! 

Congratulations! You've successfully written and executed your first Java program.

Conclusion

link to this section

In this tutorial, you learned how to create, compile, and run a simple Java program. While the "Hello, World!" program is basic, it serves as an essential first step in learning Java programming. From here, you can explore more advanced Java concepts and start building more complex applications. Happy coding!