Sunday, 19 February 2017

Writing and Executing a Java Program

Writing and Executing a Java Program:

  In high-level programming languages such as C and C++, you write a program in a human-readable format, and a program called compiler, translates it to a binary format called executable code that the computer can understand and execute. The executable code depends upon the computer machine that you use to execute your program; it is machine dependent. In Java, this process from writing to executing a program is very similar but with one important difference that allows you to write Java programs that are machine independent. To understand that difference, pay attention to the term Java virtual machine (JVM).

Writing a Java Program:

  A Java program that you write contains a set of instructions in text format written according to the rules of the Java programming language. These instructions are called source code. The file that contains these instructions is called the source file and has the file extension .java. You can use a text editor such as Notepad to create and edit Java source files.
As an exercise, open a new text document (for example, using Notepad on your Windows machine), type the code presented in bellow example into the document, and save the document as

 class HelloWorld
   public static void main(String args[])
    System.out.println("Hello World");

Compiling a Java Program:

  As you know by now, computers cannot understand the source code that you write. Before a CPU of a computer can execute the instructions written by you, the instructions need to be translated into a machine language—that is, into a binary format. In most of the programming languages, such as C and C++, a compiler compiles the source code into the machine language, and this compiled code is called executable code. However, in Java, the compiler compiles the code into bytecode, which is not executable code. Bytecode is somewhere in the middle of source code and executable code. For example, to create the bytecode files from the source file, you run the Java compiler by issuing the following command:


  The compiler converts the text instructions into bytecode form and places them in files with the .class extension: Robot.class and HelloWorld.class. One class file is created corresponding to each class in the source file.

Caution The command to compile a program in the source code file is javac, and you must include the file
extension .java when specifying the file name.The bytecode in the class files is interpreted by the JVM when you execute (or run) the program.

Executing a Java Program:

  You can execute a Java program by issuing the java command. To execute the program in our running example, issue the following command:

java HelloWorld

This generates the following output:
Hello World

  When you issue the java command, the JVM reads your bytecode file and translates the instructions to the executable format that your computer can understand. The executable form of a program is specific to a particular machine. This is why you need a specific JVM for a specific kind of platform such as Windows, Solaris, or Linux. However, as illustrated in Figure 1-2, you need to write the program only once, but you can execute it on a number of different machines with different operating systems by using different JVMs.

 Next Topic Shahbaz


Post a Comment

Powered by Blogger.