Java Example Compilation Execution

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.

21 thoughts on “Java Example Compilation Execution”

  1. Dhananjay Kumar Pal

    Sir, in the main method argument is String array, what if we simply type String in place of String []. Like public static void main (String args)

  2. public static int main(string[] args)
    {
    return 0;
    }
    the above statement will work smoothly ,it means in the above statement int is a return type that returning 0 value,that means main method can have return type .am i right sir

  3. Yashwant Singh Chauhan

    Sir I have two questions :
    1) In the command prompt when we execute the java code with the help of java interpreter we only specify the name of the class within which the “main” method is defined and do not explicitly call the “main” method with (.) operator. Then how the main method is invoked automatically?
    2) The main function takes argument of String type of array, but at the time of specifying the class file to “java” interpreter we do not pass the any kind value and still it runs smoothly. How is it possible that the program executes without passing any argument, as in case of normal methods we need to pass the arguments (if defined).

    1. 1. Suppose if you execute like this:

      c:\> java Demo

      automatically it becomes

      c:\> java Demo.class

      then Demo.class is loaded.

      Instead, if you type yourself as

      c:\> java Demo.class

      then Java adds automatically

      c:\> java Demo.class.class

      then such file does not exit in you directory, there by it gives error message.

      Java designer makes many things simple to use. Purposefully they avoided .class to put in the execution command, just to make your typing simple

      2. Along with your execution command, as no arguments are passed, the String array size becomes zero by default. It is not error, why, to program your execution simple as many times we do not pass arguments, especially in realtime applications.

    2. Many things are made simple for Java programmer. Answer for your both questions gives the proof.

      1. If you type for execution like this:

      C:\> java Demo

      automatically system adds .class extension to it and your command becomes like this:

      C:\> java Demo.class

      And if you type like this manually:

      C:\> java Demo.class

      JRE adds .class file as usual and then your command becomes like this:

      C:\> java Demo.class.class

      By the name of Demo.class.class, no file exist in your current directory and thereby the compiler raises error.

      2. When you do not pass any command-line arguments, the size of the String array becomes zero. Why so? We do not pass command-line arguments generally, especially in realtime applications.

      1. Sir,this is very good website to learn java.
        sir in the above you told main method does not have return type,its not possible in java,if that is the case .how it is possible in the bellow statement.
        public static void main(String args[])
        in the above statement void is a return type,we can also declare int as return type.it will work smoothly.

        1. void is not return type. A method/function should have three constraints to be satisfied.

          1. There should be a return type. void indicates that the method does not have return a value. The return types can be objects or primitive data types.
          2. There should be parenthesis; atleast empty parenthesis.
          3. There should body; atleast empty body.

Leave a Comment

Your email address will not be published.