One of the most fundamental programs in Java is being able to print out a certain text on the terminal. As is a conventional in Java, more often than not, ‘Hello World’ is the text that is chosen. The following program will demonstrate how to print out ‘Hello World’ by writing a simple program in Java.
Java can be simplified and broken down in the three following steps:
- Writing a simple program on a text editor and saving the file name using the ‘.java’ extension.
- Use the ‘javac filename.java’ command in the terminal to compile the code.
- Run the program by using the ‘java filename’ command in the terminal window.
Hello World Program
/*The following is a simple program that will illustrate how to print ‘Hello World’ in Java*/
//invoking the main method of the class
public static void main (String args )
- Class Definition – ‘class HelloWorld’ signifies the creation of a new class called ‘HelloWorld’
- Main Method – Every Java class must have a method. As there is no requirement of making other methods, the code has been written in the main method itself.
- Print – ‘System.out.println()’ is the syntax for printing out text to the terminal. ‘System’ is a predefined class, and ‘out’ is a variable of type output stream.
The name of the Java file and the name of the class must be the same. In this case, the name is ‘Hello World’. We have chosen the name of out method to be ‘main’. Any other name could also have been chosen however ‘main’ is a naming convention in Java.