Java Example Compilation Execution

November 2, 2010 | Java OOPs Concepts

Java Example Compilation Execution

Summary: By the end of this tutorial, you will understand how a "Java Example Compilation Execution" is done.

What is a class in Java?

Two concepts existed in the minds of Java designers from the day one of development - to make the language simple and easy to practice and platform-independent. The basic entity of Java coding is a "class". A class is delimited by a pair of braces, { and }. All the code resides within a class. The name of the class is the name with which the whole code is known. An object is a handle to manipulate the whole code of the class. A class can comprise of constructors, variables, methods and some blocks of code (like static blocks) etc.

First Java Program

Here, some procedure is to be followed. We know the basic construct of a Java program is a "class".

Open a file with the same name of that of the class. Say, the name of the class, you would like to write is "Wihses", then open the file as "Wishes.java" as follows.

c:\basics> notepad Wishes.java

The above statement creates a file called Wishes.java in the current directory c:\basics. In the notepad editor start typing the program.

import java.lang.*;
public class Wishes
{
  public static void main(String args[])
  {
    System.out.println("Hello World");
    System.out.println("Best wishes");
  }
}

Just save the file after typing the code.

Compilation

Now it is the time for compilation. Compile as follows.

c:\snr> javac Wishes.java

"javac" is a Java command to compile the file, Wishes.java. If everything goes smooth (no errors exist in the program), you obtain a file called "Wishes.class". Wishes.class is the executable file (equivalent to .exe of C/C++). Now you can open the Wishes.class and see. What you find is the bytecode format. Bytecode makes Java platform-independent.

Execution

Type the following command at the DOS prompt to execute the code.

c:\snr> java Wishes

"java" is a Java command of execution. That is, you are invoking the Java interpreter to execute the bytecode. You get the output at the DOS prompt.

Java Example Compilation Execution

Output screen of Wishes.java

Let us discuss some coding details of the above simple program.

import java.lang.*;

"import" is a keyword of Java equivalent to "include" of C/C++. Here, a small difference exists. "java.lang" is known as package. A package is equivalent to a header file of C-lang. A package contains classes and methods exist in the classes. But, a header file contains directly functions. Java classes (containing methods) are distributed through packages and sub packages. "lang" is the sub package of package "java". Distributing classes through packages has got an advantage over traditional header files. Note: "java.lang" package is implicitly imported if the programmer does not import himself.

public class Wishes

In the above statement, "public" is a keyword and denotes an access specifier. "public" means anybody can access the code without any restrictions. Java includes other access specifiers also. The whole program is known by the name "class Wishes". Again "class" is a keyword. Observe, the class is delimited by two braces, { and }. Java demands that all the code you write, may spread thousands of lines, should be within the class declaration only.

public static void main(String args[])

The main() method comes with many decorative paraphernalia and each one conveys some meaning to the compiler. The "static" means the main() method can be called without the help of an object. "void" keyword means the main() method does not return a value. The main() takes a String array as parameter and is used later to access command-line arguments.

The System.out.println() prints the data at the DOS prompt (equivalent to printf()). println() method gives an implicit "\n" character. "in" in println() stands for new line. The output comes in two different lines.