Java Hello World

Beginning Java programming with Hello World Example


Below given  program is the simplest program of Java printing “Hello World” to the screen. 


/* This is a simple Java program.
   FileName : "HelloWorld.java". */
class HelloWorld
{
    // Your program begins with a call to main().
    // Prints "Hello, World" to the terminal window.
    public static void main(String args[])
    {
        System.out.println("Hello, World!");
    }
}

Output:

Hello, World!


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

  1. Class definition:This line uses the keyword class to declare that a new class is being defined.
    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 mainmethod whose signature is:
    public static void main(String[] args)
    
    public: So that JVM can execute the method from anywhere.
    static: Main method is to be called without object. 
    The modifiers public and static can be written in either order.
    void: The main method doesn't return anything.
    main(): Name configured in the JVM.
    String[]: The main method accepts a single argument: 
              an array of elements of type String.

    Like in C/C++, main method is the entry point for your application and will subsequently invoke all the other methods required by your program.

  3. The next line of code is shown here. Notice that it occurs inside main( ).
    System.out.println("Hello, World");
    

    This line outputs the string “Hello, World” followed by a new line on the screen. Output is actually accomplished by the built-in println( ) method. 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.


    Compiling the program :

    • After successfully setting up the environment, we can open terminal in both Windows/Unix and can go to 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.


      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 contain main() method.
      • By convention, the name of the main class(class which contain main method) should match the name of the file that holds the program.

Comments

Popular posts from this blog

Solve the Sudoku Python

Solve the Sudoku Java

Find Duplicates Java