Showing posts with label hello world. Show all posts
Showing posts with label hello world. Show all posts

Sunday, 18 May 2014

What happen while execute the java program ?


Virtual machine start-up

      A Java virtual machine starts execution by invoking the method main of some specified class, passing it a single argument, which is an array of strings. 
     
d:\app1\src\>java Test

   The steps the virtual machine may take to execute Test class are loading, linking, and initialization processes that are described further in this section.


Load the Class

    To execute the main method  of  Test class discovers that the class Test is loaded or not means that the virtual machine currently contain a binary representation for this class or not .If class not loaded then ,the virtual machine uses a class loader to attempt to find such a binary representation. If this process fails, then an error is thrown.
 
Link : Verify, Prepare

   Verification checks that the code that implements Test obeys the semantic requirements of the Java programming language and the Java virtual machine. If a problem is detected during verification, then an error is thrown.
  Preparation involves allocation of static storage and any data structures that are used internally by the virtual machine, such as methods.

Initializ : Execute Initializers

  Initialization consists of execution of any class variable initializers and static initializers of the class Test.But before Test can be initialized, its direct superclass must be initialized, as well as the direct superclass of its direct superclass, and so on.

Invoke : main method

   Finally, after completion of the initialization for class Test (during which other consequential loading, linking, and initializing may have occurred), the main method of Test is invoked. The method main must be declared publicstatic, and void. It must accept a single argument that is an array of strings. This method can be declared as


public static void main(String[] args)


Friday, 16 May 2014

First Program “HELLO WORLD”



When we start reading any language like English then we have to knowledge of letter, word, sentence and at last grammar. Likewise in c language token, keyword, statement and syntax respectively. In previous post we seen all these things so let us see the first program of printing the hello world in the screen (Command prompt).


      1   #include<stdio.h>
      2
      3   int main()
      4   {
      5          printf("Hello World");
      6                        // this statement display the string Hello World
      7         getchar();
      8         return 0;         
      9    }
  

   A C program basically consists of the following parts:
  • Preprocessor Commands
  • Functions
  • Variables
  • Statements & Expressions
  • Comments

In this program; in first line '#' means a Pre-processor and "include" is used for including the header file and libraries.To know more about Pre-processor plz Click-Here.
 Here "stdio.h" is added which includes some pre defined and standard i/o function like getchar(),printf().

 In third line, we already know "int" which is data type but here it is also called as return type.Return type means which type must be return by the function. Here it is return as integer type therefore in the 8th line, it returns 0.

In fifth line, printf() is a pre-defined function which is defined in stdio.h  header file.And used for printing the "Hello World" in a console.

In the seventh line, getchar() holds the screen until the user press "enter" key only. Here we also used getch() instead of getchar() but difference is that getch() holds the screen until the user press any key.

Compile & Execute C Program:

Lets look at how to save the source code in a file, and how to compile and run it. Following are the simple steps:
  1. Open a text editor and add the above-mentioned code.
  2. Save the file as hello.c
  3. Open a command prompt and go to the directory where you saved the file.
  4. Type gcc hello.c and press enter to compile your code.
  5. If there are no errors in your code the command prompt will take you to the next line and would generate a.out executable file.
  6. Now, type a.out to execute your program.
  7. You will be able to see "Hello World" printed on the screen

    $ gcc hello.c
    $ ./a.out
    Hello, World!
Output of this program is shown as:


Thursday, 15 May 2014

Hello world program


How to develop A Hello world Progaram ?

Let's see the development of Hello world program.

Develop the program as show below.

class Test
{
     public static void main(String[] args) 
     {
        System.out.println("Hello World!");
     }
}

Here in bule color all are keywords , red are inbuilt classes and pink is the string literal.
More about keywords,identifiers and basic syntax click here.
Save this code with file name as Test.java .

Here Test is class name and .java is the file extension for java files.

e.g D:\app1\src\Test.java

Start the command prompt and follow the procedure

C:\Users\xyz>D:

D:\>cd app1\src

D:\app1\src\>javac Test.java

D:\app1\src>java Test

Hello world!

Download this File

Internal process of compilation stage click here