Saturday, 20 December 2014

Running a Java Application

When a program is compiled (C or C++ program), it is directly translated into machine code that is specific to a platform/processor. But running a java program is a two-step process. In java translation from source code to the executable code is achieved using two translators:

Java Compiler: A java program is first compiled by the java compiler to generate bytecode. Bytecode resemble machine code but it is not specific to any platform i.e. it can not be executed on a computer without further translation.


Java Interpreter: Java interpreter executes the byte code after interpreting and converting into machine code.

Example: the following java program just displays the message "Hello World" on the monitor/console.

Step1: Write the following code into file "Hello.java" using any text editor:

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

Note: Java is a case sensitive language


Step 2: Compile the above written java program using the command:
javac Hello.java

Step 3: Run the java program
java Hello


Output: Hello World



No comments:

Post a Comment