Java Hello World Program
// Java program to print "Hello, World!"
public class HelloWorld {
/**
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("Hello World");
}
}
This five-line program declares the HelloJava class and the main() method. It prints some text as output using a predefined method named println().
HelloJava executes as a command-line application within a shell or DOS window and outputs its results. We will provide HelloJava with a graphical user interface (GUI) before moving on because we find that to be a little outdated.
HelloJava may be a small program, but there is quite a bit going on behind the scenes.
Let’s break this “Java Hello World program” line by line to print Hello World.
Step 1: The first step in the Java hello world program is to create a Class.
In Java, the class name should always start with a capital letter, and the name of the Java file should be the same as the class name.
public class HelloWorld
In the above Code, Helloworld is the class name, and public is an access modifier used to define the class’s access level.
Note: Learn about Access modifiers in Java
Step 2: Main Method in Java
public static void main(String[] args)
public static void main(String[] args) is a method. A method is a block of code that can be executed in a program by calling its name.
Public is an access modifier that determines which external classes can use or call the method.
Static means that the method is class-related and not instance-related. Currently, this means that you can call static methods of a class without needing to create an instance using the “new” keyword first, as is done with the main(String[] args) method.
void is the method’s return type; as expected, void means the method returns nothing.
The method named ‘main’ is unique because the Java Virtual Machine uses it as the starting point of a program.
String[] args is an array of strings used as arguments for the primary method.
Step 3: Print Statement
System.out.println("Hello World");
System.out.println supports various objects and data types. The println method invokes the toString() method of any objects passed as an argument to display details of instances of Java classes.
Java is case-sensitive, so the System must lead with an upper-case letter S.
The println method must be in “double quotes,” not ‘single quotes.’
Performance of the Code.
Time Complexity
- The program runs a single statement, so the time complexity is O(1) (constant time).
Space Complexity
- The program does not use additional memory, so the space complexity is O(1).
Memory Usage:
Very low
Execution Speed:
Fast (constant time execution)
Since it’s a simple I/O operation, its execution is nearly instantaneous.
Code Execution Guide
Step 1: Install Java
If you don’t have Java, download it from Oracle’s site.
Step 2: Write the Code
Open a text editor (e.g., Notepad, VS Code, IntelliJ, Eclipse).
Please copy and paste the Java program provided above.
Save the file as HelloWorld.java.
Step 3: Compile the Code
Open a terminal or command prompt.
Navigate to the folder where the HelloWorld.java file is saved.
Compile the Java file using the command:
javac HelloWorld.java
This generates a HelloWorld.class file (bytecode).
Step 4: Run the Program
Execute the program using
java HelloWorld
Expected Output:
Hello, World!
Pic: Executing the Java Hello World program in Eclipse.
Classes and Objects
Running a Java program, as shown in our example, entails selecting a specific class and providing the Java virtual machine with its name as an input.
The Java command then checked our HelloJava class to verify if it has the specific method named main() in the correct format.
Applications enter through the main() function. At least one class in every standalone Java application has a main() method that initiates the rest of the program by carrying out the required operations.
A class contains the variables and methods that comprise a particular application component, acting as its blueprint.