Hello World Program in Java

by Jasleen Chhabra | Updated on 24 August 2024

Table of Contents (Click any Topic to view first)

  1.  Writing the Program
  2. Breaking Down the Code
  3. Running the Program
  4. Conclusion

Java Program to Print "Hello World"

Welcome to the world of Java programming! If you're just starting out with Java, one of the first things you'll learn is how to write a simple program that prints "Hello, World!" to the console. This classic introductory program is a great way to get familiar with the basics of Java syntax and structure. Let's dive in and write your first Java program together.

Writing the Program

public class HelloWorld {

    public static void main(String[] args) {

        System.out.println("Hello, World!");

    }

}

Breaking Down the Code

  • public class HelloWorld {: This line declares a class named HelloWorld. In Java, every program must have at least one class, and the filename must match the class name.
  • public static void main(String[] args) {: This line defines the main method, which is the entry point for any Java program. The public keyword indicates that the method can be accessed from anywhere, static means the method belongs to the class itself rather than to any specific object of the class, void specifies that the method doesn't return any value, and String[] args is an array of strings that can be passed as arguments when the program is executed.
  • System.out.println("Hello, World!");: This line uses the System.out.println() method to print the string "Hello, World!" to the console. System.out refers to the standard output stream, and println() is a method used to print a line of text.

Running the Program

To run this program, follow these steps:

  1. Open a text editor and copy the code above into a new file named HelloWorld.java.
  2. Open a command prompt or terminal window.
  3. Navigate to the directory where you saved the HelloWorld.java file.
  4. Compile the program by typing javac HelloWorld.java and pressing Enter.
  5. If the compilation is successful, run the program by typing java HelloWorld and pressing Enter.
  6. You should see the output Hello, World! printed to the console.

Conclusion

Congratulations! You've just written and executed your first Java program. This simple example demonstrates the basic structure of a Java program and introduces you to some fundamental concepts of the Java language. From here, you can continue to explore and learn more about Java programming, including object-oriented principles, data structures, algorithms, and much more. Happy coding!



FAQ

Any Questions?
Look Here.

Related Articles

Abstraction in Java

Binding in Java

Break Statement in Java

Classes & Objects in Java

Collections Framework in Java

Comments in Java

Continue Statement in Java

Control Statements in Java

Data Types in Java

Do While-Loop in Java

Encapsulation in Java

Exception Handling in Java

For-Loop In Java

If-else Statement in Java

Inheritance in Java

Introduction to Java

Java Database Connectivity (JDBC)

Java Development Tools and Frameworks

Java GUI (Graphical User Interface) Programming

Java I/O

Java Vs. C++

Methods and Constructors in Java

Multithreading in Java

Object Oriented Programming in Java

Operators in Java

Polymorphism in Java

Scanner Class in Java

Setting Up Java Environment

Static Keyword in Java

Super Keyword in Java

Switch Statement in Java

This Keyword in Java

Variables in Java

While-Loop in Java