All Java programs usually start as text files that later use to create “class” files, which are executable programs. It means that Java programs can be written in any simple text editor such as Notepad.
Follow these steps to create a simple Java program that displays a traditional greeting.
1. Open a basic text editor like Notepad and type the following code exactly as here – you will create a class named Hello.
class Hello{ }
2. Between the two curly braces of the Hello class, insert the following code to create the Hello class’s main method.
public static void main (String [] args) { }
3. Between the main method’s curly braces, add the following line of code that defines what the program will do.
System.out.println ("Hello World!");
4. Save the file in any convenient place and name it exactly like this: Hello.java – the finished program now looks like this:
For a clearer understanding of each separate part of the program, we will consider them individually.
We’ll be covering the following topics in this tutorial:
Class Definition
class Hello {}
The program name is declared after the class keyword, followed by a pair of curly braces. All program code that will define the Hello class will be placed inside these curly braces.
Definition of Method main()
The second line defines a function(method) and methods of main(), which a gateway or starting point for the program. each program a Java method starts from main() with the signature :
public static void main(String[] args)
The method must be declared as just illustrated , it should be public, static and void, must have a name and a main list of parameters must have a single parameter of type array String. Places of public and static modifiers can be exchanged. In our example, the parameter is said argument, but this is not optional parameter can have any name . most programmers choose a name args or argv.
If any of the above requirements is not met , the program will compiles , but will not be able to start and will give us a message error because it contained no starting point.
Contents of the main () Method
The Code line System.out.println(“Hello Java World”); will really work in this program. The text “Hello Java World” is a literal string of characters; that will be appear in output exactly as entered. Any literal string is written between double quotation in Java Programming Language because the string is an argument for a method, and arguments to methods are always appear within parentheses. The dot in System.out.println are used to separate the class, object, and method. The println() requires only one argument. The println() prints a line of output on the screen.
In the statement System.out.println(“Hello Java World”);, out is an object. The out object represents the screen window.
The print() is similar to the println(). The println() insert a new line after the message prints. While the print(), does not insert a new line; it remains on the same line as the output.
Here is the java code for the program HelloJavaExample :
public class HelloJavaExample { public static void main(String[] args) { System.out.println("Hello Java World"); } }
Compiling and running programs
Before running a Java program for execution, it must be compiled into a class file using the Java compiler, located in the bin subdirectory and has the name javac. It was previously described how to add the bin subdirectory to the system path so that the javac compiler can be run from anywhere on the system.
Follow the steps below to compile the program from the previous page.
1. Open a command prompt (terminal window) and change to the directory where you saved the Hello source file. java.
2. At the prompt, type javac followed by a space and the filename Hello. java with the source code and press the Enter key.
If the javac compiler finds errors in the code, it stops and displays a message indicating the nature of the error — see below for debugging problems. If the javac compiler does not find any errors, it creates a new file with the program name and the .class extension.
When the compilation process ends, focus returns to the command line without any warning message, and the program is ready to run. The Java program interpreter is an application named java located in the bin subdirectory along with the javac compiler. Since this directory has already been added to the system path, the java interpreter can also be launched from anywhere.
Follow the steps below to run a program that was compiled using the procedure on the previous page.
1. Open a command prompt (terminal window) and change to the directory where the Hello.class program file is located.
2. Type java and the program name Hello, then press the key Enter.
The Hello program starts and executes the commands described in the statements of the main method. In this case, it outputs the string Hello, world! Once completed, focus returns to the prompt again.
The process of compiling and running a Java program is usually organized in sequential steps. These steps are the same and independent of the platform on which they are executed. The screenshot below shows how to compile and run Hello on the Linux operating system progressively: