Thursday, 21 September 2017

Beginning Java programming with Hello World Example

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 simplest program of Java printing “Hello World” to the screen. Let us try to understand every bit of code step by step.
/* This is a simple Java program.
 FileName : "HelloWorld.java". */
package Com.test;

public class HelloWorld {

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

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

          }

}
Output:
Hello World
The “Hello World!” program consists of three primary components: the HelloWorld class definition, the method main and source code comments. The following explanation will provide you with a basic understanding of the code:

èClass definition: This line uses the keyword class to declare that a new class is being defined.

1.     class HelloWorld 
HelloWorld 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  }.
2.     main method: In Java programming language, every application must contain a main method whose signature is:
3.     public static void main(String[] args)
4.      
5.     public: So that JVM can execute the method from anywhere.
6.     static: Main method is to be called without object. 
7.     The modifiers public and static can be written in either order.
8.     void: The main method doesn't return anything.
9.     main(): Name configured in the JVM.
10.            String[]: The main method accepts a single argument: 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.
11.      The next line of code is shown here. Notice that it occurs inside main( ).
12.            System.out.println("Hello World");
This line outputs the string “Hello World” followed by a new line on the screen. The output is actually accomplished by the built-in print( ) method. The system is a predefined class that provides access to the system, and out is the variable of type output stream that is connected to the console.
13.      Comments: They can either be multi-line or single line comments.
14.            /* This is a simple Java program. 
15.            Call this file "HelloWorld.java". */
This is a multiline comment. This type of comment must begin with /* and end with */. For single line you may directly use // as in C/C++.
Important Points :
èThe name of the class defined by the program is HelloWorld, which is same as name of 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 main() method.
èBy convention, the name of the main class( a class which contains main method) should match the name of the file that holds the program.

Compiling the program :

èAfter successfully setting up the environment, we can open the terminal in both Windows/Unix and can go to the directory where the file – HelloWorld.java is present.

èNow, to compile the HelloWorld program, execute the compiler – javac , specifying the name of the source file on the command line, as shown:
èjavac HelloWorld.java 
èThe compiler creates a file called HelloWorld.class (in present 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
In Linux



3 comments: