Physical Address

304 North Cardinal St.
Dorchester Center, MA 02124

Getting Started with Java: Hello World Example

Java is a widely-used programming language known for its versatility and robustness. If you’re new to Java and eager to start coding, let’s dive into a classic example: Hello World.

Step-by-Step Guide

Follow these steps to create your first Java program:

  1. Install Java Development Kit (JDK):
    Before you begin, ensure JDK is installed on your system. You can download it from the Oracle website.
  2. Set Up Your Development Environment:
    Once JDK is installed, set up your preferred Integrated Development Environment (IDE). Popular choices include IntelliJ IDEA, Eclipse, and NetBeans.
  3. Create a New Java Project:
    Open your IDE and create a new Java project named HelloWorld.
  4. Write the Hello World Code:
    Inside your project, create a new Java class named HelloWorld. Replace any existing code with the following:
   public class HelloWorld {
       public static void main(String[] args) {
           System.out.println("Hello, World!");
       }
   }

Let’s break down the code:

  • public class HelloWorld: Defines a class named HelloWorld.
  • public static void main(String[] args): Entry point of the program. This method is executed when you run the program.
  • System.out.println("Hello, World!");: Outputs the string “Hello, World!” to the console.
  1. Run Your Program:
    Save the file and run the main method. You should see Hello, World! printed in the console.

Explanation

  • Class Declaration (public class HelloWorld): In Java, every program must have at least one class. Here, we declare a class named HelloWorld.
  • Main Method (public static void main(String[] args)): This method is where the execution of a Java program begins. public means this method can be called from outside the class. static allows the method to be called without creating an instance of the class. void indicates that the method does not return any value. String[] args is an array of strings that can be passed as parameters when the program is executed.
  • Printing to Console (System.out.println("Hello, World!");): System.out refers to the standard output stream, and println is a method used to print a line of text. In this case, it prints "Hello, World!" followed by a newline.

Conclusion

Congratulations! You’ve successfully created and executed your first Java program. This example demonstrates the basic structure of a Java program and introduces you to fundamental concepts such as classes, methods, and console output. As you continue your Java journey, you’ll explore more advanced topics and build upon this foundational knowledge.

Happy coding!