Programming Tutorials

The Basic Structure of a Simple Java program

By: PriyaBabu in Java Tutorials on 2006-12-11  

Let us see now how the simple java program will look like.

You can use any editor like notepad or any Java IDE for writing java programs.

/*

This is a sample java program
Save this file as Welcome.java

*/

class Welcome

    {

//  A java program will start from here.

        public static void main(String args[])

            {

              System.out.println("  Welcome to Java-Samples!!! ");

            }

}

For those who are using the IDE, you can follow the instructions have given by there.

Suppose if you are entering your program in notepad, then save this file as Welcome.java

Compiling the program

After we have written our program we need to compile and run the program. For that we need to use the compiler called javac which is provided by java.

Go to the command prompt and type the file name as shown here.

c:\>javac Welcome.java

The javac compiler will create a class file called Welcome.class that contains only bytecodes. These bytecodes have to be interpreted by a Java Virtual Machine(JVM) that will convert the bytecodes into machine codes. Once we successfully compiled the program, we need to run the program in order to get the output. So this can be done by the java interpreter called java. In the command line type as shown here.

c:\>java Welcome

So the output will be displayed as

Welcome to Java-Samples!!!

As we had seen above, when the source code has been compiled , it creates a class file with a extension of .class. Since this class file contains the bytecodes that can be interpreted by the JVM which can be resided at any platform. Remember that while running the program we are using only .class file but not the .java file. So once you got the class file you can run the same java program at any platform instead of writing the program again and again. This is the very special feature about java that 'Write once and Run anywhere'.

Looking into the program line by line

Let us closely examine each part of the program.

/*

This is a sample java program
Save this file as Welcome.java

*/

This is called comment. This is for us to enter the comments about the program for our own convenience. The contents of a comment will be ignored by the compiler. Actually java supports three styles of comments. The above one is called multi-line comment which may contain several lines. This type of comment must begin with /* and end with */.

The next line of the code in a program is

class Welcome

    {

The word class is a keyword to define a new class and Welcome is a name of the class. The class definition must begins with opening curly brace ({) and ends with closing curly brace (}). The rest of the things defined inside these braces are called member of the class. And note that all the program activities are defined inside the class.

//  A java program will start from here.

This is another type of comment. This is called single line comment starts with // and ends with end of the line. Generally we use it for brief comments.

The next line of the code in a program is

public static void main(String args[])

This line begins with main method as like functions or subroutines in other languages. The program will start execute by calling this main method. Let us see briefly about the other attributes declared in main method. However we are going to discuss in detail about this in later chapters.

The keyword public is an access specifier. The keyword static is a kind of modifier. The keyword void means that the method main() does not return any value. As we had seen before all the java program will start execute by calling the main method. If we want to pass any information to a method will be received by the variables declared within the parenthesis is called parameters. In a main() method there is only one parameter ,String args[] . args[] is a name of the parameter that is an array of the objects of data type String. String store sequences of characters and args will receive the command line arguments.

All the method in java must be start with opening curly brace ({) and ends with closing curly brace (}).

The next line of the program is

System.out.println("  Welcome to Java-Samples!!! ");

Understanding of each keyword used here will be difficult now, so just take it as this System.out.println helps to display the output in the command line. As you have probably noticed, the System.out.println statement ends with ;. All statements in java must end with semicolon. And remember that java is case sensitive. So we should be very careful about cases while coding the program. Otherwise it will lead to the serious problems.






Add Comment

* Required information
1000

Comments

No comments yet. Be the first!

Most Viewed Articles (in Java )

Latest Articles (in Java)