The Wayback Machine - https://web.archive.org/web/20241127021541/https://www.geeksforgeeks.org/java-hello-world-program/
Open In App

Java Hello World Program

Last Updated : 20 Nov, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Java is one of the most popular and widely used programming languages and platforms. Java is fast, reliable, and secure. Java is used in every nook and corner from desktop to web applications, scientific supercomputers to gaming consoles, cell phones to the Internet. In this article, we will learn how to write a simple Java Program.

Steps to Implement Java Program

Implementation of a Java application program involves the following steps. They include:

  1. Creating the program
  2. Compiling the program
  3. Running the program

For those who want to deepen their understanding of Java and master the complete development process, pursuing a comprehensive Java programming course can offer structured learning and hands-on experience, enabling you to develop efficient and scalable applications.

1. Creating Programs in Java

We can create a program using Text Editor (Notepad) or IDE (NetBeans)

class Test
{
public static void main(String []args)
{
System.out.println("My First Java Program.");
}
};

File Save: d:\Test.java

2. Compiling the Program in Java

To compile the program, we must run the Java compiler (javac), with the name of the source file on the “command prompt” like as follows

If everything is OK, the “javac” compiler creates a file called “Test.class” containing the byte code of the program.

3. Running the Program in Java

We need to use the Java Interpreter to run a program. Java is easy to learn, and its syntax is simple and easy to understand. It is based on C++ (so easier for programmers who know C++).

The process of Java programming can be simplified in three steps:

  • Create the program by typing it into a text editor and saving it to a file – HelloWorld.java.
  • Compile it by typing “javac HelloWorld.java” in the terminal window.
  • Execute (or run) it by typing “java HelloWorld” in the terminal window.

The below-given program is the most simple program of Java printing “Hello World” to the screen. Let us try to understand every bit of code step by step.

Java
// This is a simple Java program.
// FileName : "HelloWorld.java".

class HelloWorld {
    // Your program begins with a call to main().
    // Prints "Hello, World" to the terminal window.
    public static void main(String args[])
    {
        System.out.println("Hello, World");
    }
}

Output
Hello, World

1. Class Definition

This line uses the keyword class to declare that a new class is being defined.

class HelloWorld {
//
//Statements
}

2. HelloWorld

It is an identifier that is the name of the class. The entire class definition, including all of its members, will be between the opening curly brace ” { ” and the closing curly brace ” } “.

3. main Method

In the Java programming language, every application must contain a main method. The main function(method) is the entry point of your Java application, and it’s mandatory in a Java program. whose signature in Java is:

public static void main(String[] args)

Explanation of the above syntax

  • public : So that JVM can execute the method from anywhere.
  • static : The main method is to be called without an object. The modifiers are public and static can be written in either order.
  • void : The main method doesn’t return anything.
  • main() : Name configured in the JVM. The main method must be inside the class definition. The compiler executes the codes starting always from the main function.
  • String[] : The main method accepts a single argument, i.e., an array of elements of type String.

Like in C/C++, the main method is the entry point for your application and will subsequently invoke all the other methods required by your program.

The next line of code is shown here. Notice that it occurs inside the main() method.

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

This line outputs the string “Hello, World” followed by a new line on the screen. Output is accomplished by the built-in println( ) method. The System is a predefined class that provides access to the system and out is the variable of type output stream connected to the console.

Comments

They can either be multiline or single-line comments.

// This is a simple Java program. 
// Call this file "HelloWorld.java".

This is a single-line comment. This type of comment must begin with // as in C/C++. For multiline comments, they must begin from /* and end with */.

Important Points

  • The name of the class defined by the program is HelloWorld, which is the same as the name of the file(HelloWorld.java). This is not a coincidence. In Java, all codes must reside inside a class, and there is at most one public class which contains the main() method.
  • By convention, the name of the main class(a class that contains the main method) should match the name of the file that holds the program.
  • Every Java program must have a class definition that matches the filename (class name and file name should be same).

Compiling the Program

  • After successfully setting up the environment, we can open a terminal in both Windows/Unix and go to the directory where the file – HelloWorld.java is present.
  • Now, to compile the HelloWorld program, execute the compiler – javac, to specify the name of the source file on the command line, as shown:
javac HelloWorld.java 
  • The compiler creates a HelloWorld.class (in the current working directory) that contains the bytecode version of the program. Now, to execute our program, JVM (Java Virtual Machine) needs to be called using Java, specifying the name of the class file on the command line, as shown:
java HelloWorld
  • This will print “Hello World” to the terminal screen.

In Windows

Java hello world in windows

In Linux

Java hello world in linux



Previous Article
Next Article

Similar Reads

Hello World Program : First program while learning Programming
In this article, I'll show you how to create your first Hello World computer program in various languages. Along with the program, comments are provided to help you better understand the terms and keywords used in theLearning program. Programming can be simplified as follows: Write the program in a text editor and save it with the correct extension
6 min read
Writing First C++ Program - Hello World Example
C++ is a widely used Object Oriented Programming language and is relatively easy to understand. The "Hello World" program is the first step towards learning any programming language and is also one of the most straightforward programs you will learn. The Hello World Program in C++ is the basic program that is used to demonstrate how the coding proc
4 min read
Print Hello World Without using a Semicolon in Java
Every statement in Java must end with a semicolon as per the basics. However, unlike other languages, almost all statements in Java can be treated as expressions. However, there are a few scenarios when we can write a running program without semicolons. If we place the statement inside an if/for statement with a blank pair of parentheses, we don’t
2 min read
Hello World Example using Stuts2
Apache Struts 2 is an open-source web application framework for developing Java EE web applications. It uses and extends the Java Servlet API to encourage developers to adopt a model–view–controller architecture. In this article, we will see how we can create a simple Hello World application in Struts 2. We are going to use Eclipse IDE so that all
4 min read
The "Hello World" To Programming
If you have suddenly got interested to get into programming, maybe because your school finally started teaching a programming language (that too most probably during the last years of school life) and/or you surf online to find the cool stuff that people do with programming or you are fascinated by the knowledge and skill that experts have. Either
4 min read
Spring Boot - Hello World
Spring Boot is built on the top of the spring and contains all the features of spring. And is becoming a favorite of developers these days because of its rapid production-ready environment which enables the developers to directly focus on the logic instead of struggling with the configuration and setup. Spring Boot is a microservice-based framework
4 min read
Print Hello World without semicolon in C/C++
Every statement in C++ must end with a semicolon as per basics. However, unlike other languages, almost all statements in C++ can be treated as expressions. However there are few scenarios when we can write a running program without semicolon. If we place the statement inside an if/switch/while/macro statement with a blank pair of parentheses, we d
2 min read
Apache Kafka - Real World Project with Twitter using Java
Apache Kafka is a publish-subscribe messaging system. A messaging system let you send messages between processes, applications, and servers. Apache Kafka is software where topics (A topic might be a category) can be defined and further processed. To know more about this, please refer to the article - What is Apache Kafka and How Does it Work? In th
5 min read
Top 20 Java Applications in Real World [2025]
Java, a programming language introduced over 25 years ago, continues to be the cornerstone of modern software development. From mobile apps to enterprise solutions, Java powers some of the most popular and mission-critical applications worldwide. Its platform independence, robust security, and object-oriented design have made it a top choice for de
8 min read
Difference Between java.sql.Time, java.sql.Timestamp and java.sql.Date in Java
Across the software projects, we are using java.sql.Time, java.sql.Timestamp and java.sql.Date in many instances. Whenever the java application interacts with the database, we should use these instead of java.util.Date. The reason is JDBC i.e. java database connectivity uses these to identify SQL Date and Timestamp. Here let us see the differences
7 min read
Understanding OOPs and Abstraction using Real World Scenario
Object-oriented programming: As the name suggests, Object-Oriented Programming or OOPs refers to languages that use objects in programming. Object-oriented programming aims to implement real-world entities like inheritance, hiding, polymorphism etc in programming. In this article, we will discuss how this OOP's concept is implemented in real world
4 min read
Why the World is Moving Towards Microservices Architecture?
Imagine you run an organization called XYZ and let's say you have a requirement right now that you want to create an "Employee and Customer Management System". And let's say you have few people who purchase your services like your clients/customers. So they talk with you about the requirements and all. And in the organization, you have some employe
7 min read
Java AWT vs Java Swing vs Java FX
Java's UI frameworks include Java AWT, Java Swing, and JavaFX. This plays a very important role in creating the user experience of Java applications. These frameworks provide a range of tools and components for creating graphical user interfaces (GUIs) that are not only functional but also visually appealing. As a Java developer, selecting the righ
11 min read
Output of Java Program | Set 1
Difficulty Level: Rookie Predict the output of the following Java Programs.Program 1: Java Code // filename Main.java class Test { protected int x, y; } class Main { public static void main(String args[]) { Test t = new Test(); System.out.println(t.x + " " + t.y); } } Output: 0 0 In Java, a protected member is accessible in all cl
3 min read
Output of Java Program | Set 2
Predict the output of the following Java programs. Question 1: Java Code package main; class Base { public void Print() { System.out.println("Base"); } } class Derived extends Base { public void Print() { System.out.println("Derived"); } } class Main { public static void DoPrint(Base o) { o.Print(); } public static void main(Str
3 min read
Output of Java Program | Set 3
Predict the output of the following Java Programs: Example1: Java Code // filename: Test.java class Test { // Declaring and initializing integer variable int x = 10; // Main driver method public static void main(String[] args) { // Creating an object of class inside main() Test t = new Test(); // Printing the value inside the object by // above cre
3 min read
Output of Java program | Set 23 (Inheritance)
Prerequisite: Inheritance in Java 1) What is the output of the following program? Java Code public class A extends B { public static String sing() { return "fa"; } public static void main(String[] args) { A a = new A(); B b = new A(); System.out.println(a.sing() + " " + b.sing()); } } class B { public static String sing() { retu
3 min read
Java program to expand a String if range is given?
Suppose we have given a string in which some ranges as specified and we have to place the numbers which is between the given range in the specified place as provided and depicted in the illustration below as follows for a better understanding. Illustration: Input : string x = "1-5, 8, 11-14, 18, 20, 26-29" Output : string y = "1, 2, 3, 4, 5, 8, 11,
6 min read
Compilation and Execution of a Java Program
Java, being a platform-independent programming language, doesn't work on the one-step compilation. Instead, it involves a two-step execution, first through an OS-independent compiler; and second, in a virtual machine (JVM) which is custom-built for every operating system. The two principal stages are explained below: Principle 1: Compilation First,
5 min read
Java Program to Convert ArrayList to LinkedList
Given an array list, your task is to write a program to convert the given array list to Linked List in Java. Examples: Input: ArrayList: [Geeks, forGeeks, A computer Portal] Output: LinkedList: [Geeks, forGeeks, A computer Portal] Input: ArrayList: [1, 2, 3, 4, 5] Output: LinkedList: [1, 2, 3, 4, 5] ArrayList - An ArrayList is a part of the collect
6 min read
Program to convert a Set to Stream in Java using Generics
Java Set is a part of java.util package and extends java.util.Collection interface. It does not allow the use of duplicate elements and at max can accommodate only one null element. A Stream is a sequence of objects that supports various methods which can be pipelined to produce the desired result. Below are the various methods to convert Set to St
3 min read
Program to Convert Milliseconds to a Date Format in Java
Given milliseconds. The task is to write a program in Java to convert Milliseconds to a Date that Displays the date in dd MMM yyyy HH:mm:ss:SSS Z format.The Date class in Java internally stores Date in milliseconds. So any date is the number of milliseconds passed since January 1, 1970, 00:00:00 GMT and the Date class provides a constructor which c
2 min read
Java program to remove nulls from a List Container
List is an ordered collection of objects which allows to store duplicate values or null values, in the insertion order. So it is very important to remove null values in many scenarios. Examples: Input: [Geeks, null, forGeeks, null, A computer portal] Output: [Geeks, forGeeks, A computer portal] Input: [1, null, 2, 3, null, 4] Output: [1, 2, 3, 4] B
6 min read
Program to Convert List to Map in Java
The List is a child interface of Collection. It is an ordered collection of objects in which duplicate values can be stored. Since List preserves the insertion order, it allows positional access and insertion of elements. List Interface is implemented by ArrayList, LinkedList, Vector and Stack classes. The java.util.Map interface represents a mappi
5 min read
Java Program to convert Character Array to IntStream
Given a Character Array, the task is to convert this array into an IntStream containing the ASCII values of the character elements. Examples: Input: char[] = { 'G', 'e', 'e', 'k', 's' } Output: 71, 101, 101, 107, 115 Input: char[] = { 'G', 'e', 'e', 'k', 's', 'F', 'o', 'r', 'G', 'e', 'e', 'k', 's' } Output: 71, 101, 101, 107, 115, 70, 111, 114, 71,
1 min read
Program to convert IntStream to String in Java
Given a Instream containing ASCII values, the task is to convert this Instream into a String containing the characters corresponding to the ASCII values. Examples: Input: IntStream = 71, 101, 101, 107, 115 Output: Geeks Input: IntStream = 71, 101, 101, 107, 115, 70, 111, 114, 71, 101, 101, 107, 115 Output: GeeksForGeeks Algorithm: Get the Instream
1 min read
Program to convert String to IntStream in Java
Given a String, the task is to convert this String into an IntStream containing the ASCII values of the characters as the elements. Examples: Input: String = Geeks Output: 71, 101, 101, 107, 115 Input: String = GeeksForGeeks Output: 71, 101, 101, 107, 115, 70, 111, 114, 71, 101, 101, 107, 115 Algorithm: Get the String to be converted. Convert it in
1 min read
Program to convert Byte Array to Writer in Java
References: Writer Class Approach: Writer class is used to write character stream, by which byte array can be passed as an argument. By this way, byte array can be converted into Writer class. To get the byte array from String, getBytes() method is used. Below is the implementation of the above approach: Program: // Java Program Convert // Byte Arr
2 min read
Java Program to Convert Iterator to Spliterator
Given an Iterator, the task is to convert it into Spliterators in Java. Examples: Input: Iterator = {1, 2, 3, 4, 5} Output: {1, 2, 3, 4, 5} Input: Iterator = {'G', 'e', 'e', 'k', 's'} Output: {'G', 'e', 'e', 'k', 's'} Approach: Get the Iterator. Convert the iterator to Spliterator using Spliterators.spliteratorUnknownSize() method. Return the Split
1 min read
Java Program to Convert String to Date
Given a string in date format, the task is to convert this String into an actual date. Here the main concept is the parse() method which helps in the conversion. Illustration: Input : string = "2018-10-28T15:23:01Z" Output: 2018-10-28T15:23:01Z Input : string = "28 October, 2018" Output: 2018-10-28Different Methods to Convert String to DateUsing in
4 min read
Practice Tags :
three90RightbarBannerImg