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

Modern Programming Tools and Techniques I



                                                 System.out.println("My first program in Java");
                                               }
                                          }
                                          In this example,
                                              (a)    A class FirstProgram is created using the class keyword.
                                              (b)   In the class FirstProgram, the main( ) method of the class is called.
                                              (c)    In this main( ) method, System.out.println("My first program in Java");
                                                 statement is used to print the output "My first program in Java" on the
                                                 screen.
                          The process to compile and run the above-given example is described in the following steps
                          1.   In the command prompt, type the directory name, and the file name.

                                          c:\jdk\programs>javac FirstProgram.java

                                          Here,  c:\  is the drive, wherein  jdk  directory is stored.  The  ‘programs’  is the
                                          directory,  where the program which is to be run is saved with the name
                                          FirstProgram.java.

                          2.   After this step is followed by the programmer, a file called as FirstProgram.class is created by the
                              compiler in the directory programs (check that it is there!). This class created by the compiler has
                              the program but in bytecode form, which is  ready to run. Run the program as given in the
                              example below.

                                          C:\jdk\programs > java FirstProgram


                                          The output of this program is:
                                          My first program in Java
                          These steps, if followed properly, will give the desired output to the user.

                          Compilation
                          At  the command line on UNIX and DOS shell operating systems, the Java compiler for the example
                          provided in section 1.2 is invoked as follows:

                                                javac ExampleProgram.java
                          Here, javac is the command and ExampleProgram.java is the name of the class (or program file).


                                      In Java, the class name and the file name must be the same. Also, since Java is case

                                      sensitive, one should always  provide same case letters for both file name and class
                                      name.
                          Once the program is successfully compiled into Java bytecode, it is ready to be interpreted and run on
                          any JVM. Interpreting and running of a Java program involves invoking the JVM bytecode interpreter.
                          It transforms the Java bytecode to platform-dependent machine codes so the computer can understand
                          and run the program.





                          6                       LOVELY PROFESSIONAL UNIVERSITY
   8   9   10   11   12   13   14   15   16   17   18