• Articles
  • Tutorials
  • Interview Questions

Hello, World! Program in Java

Hello, World Program in Notepad:

Open Notepad and type the following code:

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

Save the file as Hello.java. You can do this by clicking on File > Save As and then selecting the directory where you want to save the file. Make sure to type the filename as Hello.java and select All Files in the Save as type dropdown menu.

Open the command prompt and navigate to the directory where you saved the Hello.java file.

Compile the program by typing javac Hello.java and pressing Enter.

If there are no errors, run the program by typing java Hello and pressing Enter. The program will output “Hello, world!” in the console.

java -cp /tmp/jLRff3bu2p HelloWorld
Hello World

Output
Hello World

Java Hello World Program Explanation

  • class: The class keyword is used to declare a class in Java.
  • public: The public access modifier makes the class visible to all.
  • static: The static keyword means that the method can be called without creating an object of the class.
  • void: The void return type means that the method does not return any value.
  • main: The main method is the entry point of the program.
  • String[] args: The System.out.println() method is used to print a string to the console.
  • System.out.println(): The System.out.println() method is used to print a string to the console.

Learn Java

Java Comments

It is useful to place a comment in place to indicate what you are doing. It is added into the program for making the program easier to understand and these are not compiled by the compiler or interpreter. It is useful if you want someone else to be able to ever read your code.

There are two ways to declare comment in java which are as follows:

  • Single Line comment
  • Multi-Line comment

Single Line comment in Java:

It is used to apply the comment on a single line. To apply single line comment // is used.

Example:

System.out.println("Hello World");  // It will print Hello World

Multi-Line comment in Java:

It is used to apply the comment on multiple lines.

Example:

System.out.println("Hello World");  /*  It will print
Hello World */

Keywords in Java Programming

Keywords are also known as reserved words which express a special meaning to the compiler and interpreter. Keywords in Java are –

abstract assert boolean break
byte case catch char
class const continue default
do double else enum
extends final finally float
for goto if implements
import instanceof int interface
long native new package
private protected public return
short static strictfp super
switch synchronized this throw
throws transient try void
volatile while

Course Schedule

Name Date Details
Python Course 14 Dec 2024(Sat-Sun) Weekend Batch View Details
21 Dec 2024(Sat-Sun) Weekend Batch
28 Dec 2024(Sat-Sun) Weekend Batch

About the Author

Senior Consultant Analytics & Data Science

Sahil Mattoo, a Senior Software Engineer at Eli Lilly and Company, is an accomplished professional with 14 years of experience in languages such as Java, Python, and JavaScript. Sahil has a strong foundation in system architecture, database management, and API integration.