Wednesday, December 15, 2010

A Simple Java Application

  1. class testing
  2. {
  3.      public static void main(String args[])
  4.      {
  5.            System.out.println("Hello World ");
  6.       }
  7. }
Line 1 declares class name as testing.
Line 3 is where the programs starts to executes.The Java Technology interpreter must find this defined exactly as given or it refuses to run the program.

public: 
The method main() can be accessed by anything including the java technology interpreter.

static:
This keyword tells the compiler that the main() method is usable in the context of the class testing .No instance of the class is needed to execute static methods.

void:
This keyword indicates that the method main()  does not return any value.This is important because the Java programming language performs careful type-checking to confirm that the methods called return the types with which they were declared .

String args[]:
This method declares single parameter to the main method,args and has the type of a String array.When this method is called the args parameter contains the arguments typed  on the command line following the class name:
eg: java testing args[0] args[1]..............



Executing java program:
1:Write the program in notepad and  Save the program with its class name containing main() method.
        e.g : The above example is saved by its class name i.e testing 
    SYNTAX:
            class name.java 
    This .java is extension which indicates that the is file is java file.
    e.g: testing.java


Steps for saving Java  programs:

                                   a: A program written in notepad
                                    
Then click on files in that go to save as tab and click on it the below window will b opened.Then make a new folder rename it as shown in the figure (b) i.e java programs [marked in circle].

                                 b: making a directory to save the java program

Now in java programs directory type your CLASS NAME.java [i.e testing.java] near file name & make ALL FILES in  save as type  box. 

                                   c: saving program in a particular directory

Then click on save to save the program.

                                           d: programed saved .

Steps for executing Java programs:

The execution of Java programs are done in COMMAND PROMPT.
To open command prompt click start menu -run-in run write cmd and enter the following window will be opened.Then you have to give the path of java bin of jdk. the steps are as follows 
Go to My Computer - C drive - programs files - java - jdk1.3 or jdk1.6[any version of jdk] - bin 
this is the path just copy this path from address bar .
Then in command prompt type set path= paste the copied path.as shown below
 

 to go in other directory write directory name and give colon i.e d: and then enter  as shown in the following figure
      
To compile a java program : we use javac command to compile the java program.The steps are as follows

                                       Compiling a java program .

                                         running a java program

  For running java program after compilation just type java ani class name as shown in above figure.The output in above figure is Hello world                                 




2]


/* Program to demonstrate the addition and subtraction of double numbers*/


class AddSubDoubles
{

     public static void main (String args[]) 
    {

        double x = 7.5;
        double y = 5.4;

        System.out.println("x is : " + x);   
    System.out.println("y is : " + y);
 
        double z = x + y;
        System.out.println("x + y is : " + z);
   
        z = x - y;
        System.out.println("x - y is : " + z);

     }

 }


3]

/* Program to demonstrate the addition and subtraction of integer numbers*/



class AddSubInt
{

  public static void main (String args[])
  {
 
      int i = 5;
      int j = 3;

      System.out.println("i is " + i);
      System.out.println("j is " + j);
 
      int k = i + j;
      System.out.println("i + j is " + k);
   
      k = i - j;
      System.out.println("i - j is " + k);

  }

}

No comments:

Post a Comment