Monday, 24 March 2014

Implementing your first Java program


Implementation of java program involve the following steps:-

  • Creating the program
  • Compiling the program
  • Run the program
  • Creating the program:-To create the java program your can use text editor(like Notepad in windows).Open your text editor which you are going to use and type the following code in that:-
  • class MN
    {
     public static void main(String arg[])
     {
      System.out.println("Hello and Welcome to java programming");
     }
    }

    Save the file with MN.java (MN should be the name of file as the class name is name is MN)Name as in this screen short
    • Explanation:- 
      • Class Declaration:-The first line
        class MN
        
        This line declares a class .It is the first step to object-oriented programming.We know that Java is Object Oriented Programming ,so everything is is inside the class (class Name is MN here ).The keyword class is used to declare a class.

      • Opening Brace:-the braces { } are used to group the class definition.
        class MN
        {
        
        }
        
      • Main line-The line
        public static void main(String args[])
        
        In this line main is a method ,like we have in C and C++ main().In every Java application there must be main()method .Because it is the starting point of program execution by the interpreter.A Java program have only one main() method.With main() method there are other keywords are used explanation of them are as following:-
        public The keyword public is a access specifier that declares the main method accessible to outer classes this is because the Java program execution is started by the Operating System so main method is accessible to the Operating system.
        static The keyword static declares the main() method as one that belog to the entire class not a part of the object of the class,main method is declared as static so that Java interpreter can use the main method before any object is created of that (here MN) class in which the main is written.
        void The type modifier void states that the main() method will not return any value to the Operating system
        String args[] This is a parameter that is passed to the main() method ,at this movement just remember that it will hold the values that we will pass to the program when we will run the program
      • Output Line-The line
        System.out.println("Hello and Welcome to java programming");
        
        This is Similer to the printf() statement in C and C<< in C++.Similarly in Java System.out.println() is available.Because Java is truly Object Oriented Programming Language. So in this statement System is a class ,out is static member of System class and println() is a method of member out.
  • Compiling the program:- Now Open the cmd prompt in your system ,goto the disk place where you have stored the program(like in the picture I have stored it on the Desktop and type javac MN (MN is the class Name that I have written in the code )
  • Run the program:- Now Open the cmd prompt in your system and type the code (after javac MN) java MN:-
You will Get the message "Hello and Welcome to java programming" that is written in the program.

No comments:

Post a Comment