Page 11 - DCAP501_Modern Programming Tools and Techniques I
P. 11

Modern Programming Tools and Techniques I



                          1.2   Programming in Java

                          Any Java program starts with the class name and every class definition starts with an opening curly
                          brace ({) and ends with the corresponding curly brace (}). After the opening brace, the main line of the
                          program begins. For example:
                                                public static void main (String args[ ])
                          This line defines the main method of the program. Then, the output line is added to the program.

                                                                          System.out.println (“Hello”);
                          This statement is used to print the output on the screen.

                                          Program to print “Programming”.

                                          Class ExampleProgram
                                          {
                                               public static void main (String args[ ])
                                               {
                                                   System.out.println(“I am a simple program”);
                                               }
                                          }


                                          Output:
                                          I am a simple program.

                                          In this example,
                                           (a)   A class ExampleProgram is created using the keyword class.
                                           (b)   In the class ExampleProgram, the main( ) method of the class is called,
                                                   using the public static void main (String args[ ]) statement.
                                            (c)  Then, in the main( ) method, System.out.println(“I am a simple

                                                   program”); statement is used to print the output “I am a simple
                                                   program” on the screen.


                          1.2.1   Structure of a Java Program
                          A Java program is divided into six sections, as given in the table 1.1.

                                                      Table 1.1: Structure of a Java Program

                                       Comment Lines’ Section                                              (Suggested Section)
                                       Package Section                                                              (Optional Section)
                                       Import Statements’ Section                                           (Optional Section)
                                       Interface Statements’ Section                                        (Optional Section)
                                       Class Definitions’ Section                                              (Optional Section)
                                       Main Method Class Section                                           (Essential Section)






                          4                       LOVELY PROFESSIONAL UNIVERSITY
   6   7   8   9   10   11   12   13   14   15   16