Java "Hello World" Program

One of the most well-known examples of a first Java program is the renowned "Hello World!" program. To begin writing your initial Java program, it is essential to ensure the proper download and installation of the JDK (Java Development Kit). The JDK provides the necessary tools, including the Java compiler and runtime environment, to create, compile, and execute Java applications.

Install Java Development Kit (JDK)

To write and compile Java programs, you need to have the Java Development Kit (JDK) installed on your computer. You can download the latest version of JDK from the official Oracle website or OpenJDK for open-source alternatives. The following link will guide you to download and install JDK on your system:

Click here Downloading and installing JDK software

Set Up the Development Environment

After installing JDK, set up your development environment. You'll need a text editor to write Java code. Popular text editors or Integrated Development Environments (IDEs) like Eclipse, IntelliJ IDEA, or Visual Studio Code can be used.

Write the Java Code

Open your text editor or IDE and create a new file. Name the file with a .java extension, such as "HelloWorld.java". In this file, write the following Java code:

public class HelloWorld { public static void main(String[] args) { System.out.println("Hello, World!"); } }
Let's understand the code:
  1. public class HelloWorld: This is the class definition. In Java, every program must have a class, and the class name must match the file name (without the .java extension). The public keyword indicates that the class is accessible from any other class.
  2. public static void main(String[] args): This is the main method. It is the entry point of the Java program and is where the program starts executing. It must have this exact signature to be recognized as the starting point by the JVM.
  3. System.out.println("Hello, World!");: This line prints "Hello, World!" to the console. The System.out.println() method is used to display output in Java.

Save the File

Save the "HelloWorld.java" file in your desired location.

Compile the Java Code

To convert the Java source code (HelloWorld.java) into bytecode that can be executed by the JVM, you need to compile it. Open a command prompt or terminal and navigate to the location where you saved the file. Use the javac command to compile the code:

javac HelloWorld.java

If there are no errors in your code, the compiler will generate a new file named "HelloWorld.class", which contains the bytecode.

Run the Java Program

Once the code is compiled successfully, you can execute the Java program using the java command followed by the class name:

java HelloWorld

You should see the output in the console:

Hello, World!

Congratulations! You've just created and executed your first "Hello World" program in Java. This simple program demonstrates the basic structure of a Java program and how to print a message to the console.