Here’s an example of programming Hello World in java. A Hello World program is usually one of the first programs that is written by someone new to software development or new to programming in a particular language.
This is a common simple program and is used to show the most basic implementation and structure of a program that is possible in a language. All that a Hello World program will do is start the program and then print Hello World to the screen.
A Simple Hello World Program
Here’s a Hello World program in java below. Let’s break down what’s going on here.
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World");
}
}
Java Class Declaration
The first line defines the class. Java is an object oriented programming language, and in the java implementation of object oriented program languages there are some rules
- All code should be in a class.
- The code should be in a class where the first class is the same as the filename prefix.
- So the line below tells java we are creating a class called HelloWorld.
public class HelloWorld {
Why We Need A Public Static Void Main In Java
The next lines defines the entry point to call the code from the java virtual machine (that runs the code). When this code is called from the JVM it will look for number of things.
- There must be a method called main.
- That method must be static so that it can be accessed at the class level (java can run static methods at the class or instance level, but not static methods can only be run by creating an instance of a class.
- It must have a void return type and accept a string array as input parameters.
public static void main(String[] args) {
Print Hello World
The next line is the action we want to take when this method is called. In this case it’s very simple and we want to print a line saying “Hello World” to the console.
System.out.println("Hello World");