programmingJava Programmingbeginner's guide

Your First Java Program: Hello, World!

By Swann
Picture of the author
Published on
Your First Java Program

Introduction

The 'Hello, World!' program is a conventional starting point for learning any programming language. In Java, this simple program allows you to understand the basic structure, syntax, and execution flow of Java code. Let's delve into creating your first Java program.

Structuring a Java Program

A typical Java program consists of:

  • Class Declaration: A user-defined blueprint or prototype from which objects are created.
  • Main Method: The entry point of the program where the execution begins.
  • Statements: Commands to be executed.

Writing the 'Hello, World!' Program

Step 1: Create a Java Class

Create a new file named HelloWorld.java. The class name should match the filename.

Step 2: Write the Code

Inside the HelloWorld.java file, write the following code:

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

Here's a breakdown of the code:

  • public class HelloWorld: Declares a class named HelloWorld.
  • public static void main(String[] args): Defines the main method where the program starts execution.
  • System.out.println("Hello, World!");: Prints "Hello, World!" to the standard output.

Step 3: Compile the Code

Navigate to the directory containing HelloWorld.java using the terminal or command prompt and compile the code using the Java compiler:

javac HelloWorld.java

This generates a bytecode file named HelloWorld.class.

Step 4: Run the Program

Execute the compiled code using the Java Virtual Machine (JVM):

java HelloWorld

The output should display:

Hello, World!

Understanding the Code Structure

  • Class: HelloWorld is a public class, meaning it can be accessed from other classes.
  • Main Method: public static void main(String[] args) signifies the main method, which is the program’s entry point. It must be declared public and static.
  • Print Statement: System.out.println() is a method used to print data, in this case, a string, to the standard output.

Next Steps

With your first Java program successfully written, compiled, and executed, you are now on the path of Java programming. The next steps involve understanding more about data types, variables, operators, control structures, and diving deeper into Java programming concepts.


Additional Resources

Stay Tuned

Want to become a Next.js pro?
The best articles, links and news related to web development delivered once a week to your inbox.