Table of contents
1.
Introduction
2.
Requirements
3.
Java "Hello, World!" Program
3.1.
Program
3.2.
Java
3.3.
Compile
3.4.
Run
3.5.
Output
4.
Parameters used in First Java Program
5.
Explanation
6.
Steps to Implement Java Program
7.
Frequently Asked Questions
7.1.
How do I run a Java program in Eclipse?
7.2.
Which Java IDE is best for beginners?
7.3.
Can Vscode run Java?
8.
Conclusion
Last Updated: Sep 11, 2024
Easy

Java Hello World Program

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

In this article, we will explore how to write a simple "Hello World" program in Java. A Hello World program is the easiest way to introduce beginners to a new programming language. Along with this, we will also cover Java's essential building blocks, such as access modifiers and types of functions, to help you understand the basics of Java programming.

hello program in java

Also Read About, Multithreading in java, and Duck Number in Java.

Requirements

The following software or application must be installed before running any Java program:

  1. Install the Java Development Kit. Download and install the JDK if you haven't already done so.
  2. Set the jdk/bin directory's path (in Windows).
  3. Create a Java program.
  4. Compile and run the Java program.
     

Also check out Addition of Two Numbers in Java here.

Java "Hello, World!" Program

The Java "Hello, World!" program is a simple and fundamental program that every beginner starts with when learning Java. It prints the message "Hello, World!" on the screen. This basic program introduces you to the structure of a Java program, including key components like classes, methods, and the main() method, where the execution begins.

Let us now head over to the program to print “Hello, World” on the output screen.

Program

  • Java

Java

// HelloWorld.java


class HelloWorld {
   public static void main(String[] args) {
       System.out.println("Hello, World!");
   }
}
You can also try this code with Online Java Compiler
Run Code

Compile

Open the command prompt (or terminal in linux) and navigate to the directory where the file HelloWorld.java is saved. Then run the following command to compile the Java program.

javac HelloWorld.java

Run

java HelloWorld

Output

Hello, World!

Parameters used in First Java Program

  • Comments
    Comments are lines in the program that help explain the code but are ignored by the compiler. They begin with // for single-line comments and /* */ for multi-line comments.
     
  • Keywords
    Reserved words like class, static, and void are keywords in Java. They have specific functions, such as defining classes, methods, or access levels, and cannot be used as variable or class names.
     
  • Class Declaration
    Every Java program requires a class. The class keyword is used to define a class, and the class name should match the file name if it’s a public class.
     
  • Main Method
    The main() method is the entry point of every Java application. It is declared as public static void main(String[] args).
     
  • Static Keyword
    The static keyword allows methods and variables to belong to the class instead of instances. In the case of main(), it ensures that the method is invoked without creating an object of the class.
     
  • Void Keyword
    The void keyword indicates that the main() method does not return any value.
     
  • System.out.println()
    This statement is used to print output to the console. System is a predefined class, out is the output stream, and println() is a method to print the text.

These parameters define the basic structure and functionality of the Java program.

Explanation

  1. Comments: Any line in Java that begins with // is a comment. Comments are included in the code to help users understand the program's aim and operation. The Java compiler completely disregards it (an application that translates a Java program to Java bytecode).
  2. Keywords: Reserved words are another name for Java keywords. Keywords are specific terms that serve as a code's key. As these are Java's predefined words, they can't be used as variable, object, or class names. We saw a bunch of keywords in the above program:
    1. class: It is used to declare a class in Java. Remember that every Java program has a class definition, and the name of the public class should match the filename in Java.
    2. static: Static keyword is meant for attaching parameters (variables and methods) of a class to the class itself rather than to its objects. As the main() method is static, there is no need to create an object to call it.
    3. void: It is the return type of the method. It means it doesn't return any value.
  3. Access Modifiers: Access modifiers in java are used to set the accessibility of a data member. There are four accessibility modifiers – public, private, default and protected. We have curated a blog specifically for access modifiers here.
  4. System.out.println(): To print a statement in Java, use System.out.println(). System is a class, out is a PrintStream class object, and println() is a PrintStream class method. You can read more about java methods here.

Check out this article - Upcasting and Downcasting in Java

Must Read: Java System Out Println

Steps to Implement Java Program

  • Install the JDK (Java Development Kit)
    To run Java programs, you first need to install the JDK on your computer. The JDK includes everything needed to write and run Java code.
     
  • Write the Java Program
    Open a text editor or an IDE like Eclipse or IntelliJ and type your Java code. For a "Hello, World!" program, the code includes a class, a main() method, and a print statement.
     
  • Save the Program
    Save your file with the .java extension. Make sure the file name matches the class name. For example, if your class is named HelloWorld, save the file as HelloWorld.java.
     
  • Open Command Prompt or Terminal
    Use the command prompt (Windows) or terminal (macOS/Linux) to navigate to the folder where your Java file is stored.
     
  • Compile the Program
    Compile the program using the javac command, which translates your code into bytecode. Example:
javac HelloWorld.java


If there are no errors, this will create a HelloWorld.class file.

  • Run the Program

To run the compiled program, use the java command. Example:

java HelloWorld


This will execute the program.

  • View Output
    After running, you will see the output in the console:
Hello, World!

Frequently Asked Questions

How do I run a Java program in Eclipse?

Step 1: Launch Eclipse and select File > New > Java Project from the File menu.

Step 2: Type in the name of the project and click the Finish button.

Step 3: Select the project you created in the Package Explorer (left-hand side of the window).

Step 4: Select New > Class from the submenu when right-clicking on the src folder. Click the Finish button after entering the Class name.

Step 5: Create and save the programme.

Step 6: Now hit Ctrl+F11 or pick Run from the Run menu, or click the Run button.

Which Java IDE is best for beginners?

There are various great text editors available for Java Programming, for example, IntelliJ IDEA. BlueJ. Oracle JDeveloper, Eclipse, etc.

Can Vscode run Java?

You can use VS Code to read, write, run, and debug Java source file(s) without creating a project.

Conclusion

We discussed the following points in the blog:

  1. A class declaration that matches the filename is required for every valid Java application (class name and file name should be the same).
  2. The main method must be defined within the class.
  3. Starting with the main function, the compiler executes the code.
  4. We saw the role of Access Modifiers and Keywords in Java.
     

Recommended Readings:

 

You can also consider our paid courses such as DSA in Java to give your career an edge over others!

Live masterclass