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

Unit 1: Introduction to Java



               Let us understand the six sections of Java program in detail:
               1.   Comment Lines’ Section: This section consists of a collection of comment lines, which includes the
                    name, author and other details of the program.

               2.   Package Section: This section consists of the first statement that is permitted in a Java file. This
                    first statement is called as the package statement. It gives the name of the package and provides
                    information to the compiler that all the classes defined in the respective program are related to this
                    package.


                               package employee;

                               In this statement, package is the keyword and employee is the package name.

               3.   Import Statements’ Section:  This section consists of import statements, which can be  used for
                    accessing the classes that are related to other packages.

                               package employee.salary;

                               In this statement, the interpreter is instructed to load the  salary  class that is
                               contained in the package employee.

               4.   Interface Statements’ Section: This section consists of interface statements that are used only if the
                    multiple inheritance feature is to be implemented in the program.
               5.   Class Definitions’ Section:  This section consists of the definition of the classes used in a program.

               6.   Main Method Class Section:  This section consists of the definition of the main method in the
                    program. The main method enables the creation of objects of different classes  and the
                    communication between these objects.

               After understanding the basic elements, that is, keywords, variables, constants, and  data types, a
               programmer can easily implement this structure while developing programs in Java.
               1.2.2   Compiling, Interpreting and Running the Program

               It is necessary to convert the program into a format that can be understood by the JVM, before any
               computer with a JVM can interpret and run the  program. The process of compiling a Java program
               involves obtaining the programmer-readable text in the program file (source code) and translating it to
               bytecode. Bytecode are nothing but the platform-independent instructions for the JVM.
               After compiling and interpreting, a program is run. To run a program, follow the instructions given
               below:
               1.   Go to command prompt.

               2.   Select the drive (and the folder) where the program file is saved.
               3.   Compile, Interpret, and Run the program.
               To further understand these instructions, refer to the following example.

                               Type the following program and save it in a file with the name "FirstProgram.java"


                               class FirstProgram
                               {
                                     public static void main(String args[ ])

                                    {



                                        LOVELY PROFESSIONAL UNIVERSITY                           5
   7   8   9   10   11   12   13   14   15   16   17