Tuesday, July 22, 2014

First Simple Java Program

I will write down the simple java program first and will explain each and every line of this program followed by it.

First Simple Java Program


/*  
    This is a simple Java Program.
    Call this file 'Example.java'
*/

class Example
{

      // Your program begins with a call to main() method.

      public static void main(String args[])
      {
        
             System.out.println("This is a simple java program");

       }

}


Output of the above program is: This is a simple java program


Step by Step explanation of the above program:


The program beings with the following lines.

   /* 
         This is a simple Java Program.
         Call this file 'Example.java'
   */ 

This is a multiline comment which starts with /* and ends with */. All the lines between /* and */ will be treated as comments and wont be executed by JVM. This syntax is mainly used if there are more than one lines of comments.

   class Example
   {
        
    }

We can clearly understand from the code that we have created a class named Example. In Java, all the code must reside inside a class by writing the code inside the braces i.e. { and } of the class.

Note - The name of the class should match the name of the file that holds this program. In this example, the name of the class is Example , so while saving this program having the class Name as Example, we've to save the program file as Example.java. If we save the program file with a different name then your program wont execute. And also observe that if the class name is Example and you are going to save the program file as example.java, the program wont run as the class name and program file name wont match in case.

    // Your program begins with a call to main() method

This is a single line comment that starts with // and ends at the end of the line. JVM wont execute the single line comments.

   public static void main(String args[])
   {

    }

This is the line at which the program will begin execution. All the Java applications will begin execution by calling main() method. There can be more than one method inside a class but the program execution starts from main() method only. The main method may further call the other methods in the class for execution.

Also we've to understand the usage of public, static, void and String args[] terms in this simple program.

  • public - This means we've specified the main( ) method as public method. All the members of the class (i.e. methods and instance variables) which are preceded by public access specifier can be accessed by code outside the class. In this example main( ) method is preceded by public access specifier, hence it can be accessed by the code outside the main( ) method. We've to always use public access specifier before the main( ) method, because the main( ) method should always be accessed from outside the class by Java Virtual Machine (JVM).
  • static - In order to call any method from outside the class, we've to create an instance (creating object) for the  class in which the method is available. We have to create an instance where ever we want to use this method. But by specifying static keyword before any method, we can use the methods outside the class without creating any instances (i.e. without creating objects for the class in which the method is available). Since main( ) method  is called by the JVM before any objects are created, we've to specify static keyword in order to use the main() method.
  •  void - Using methods we can return values. For example there is a method called addition method which contains the code to add two numbers. When ever this method is called, the addition method can return sum of the two given numbers. The returned sum can be used by the method which called the addition method. But by specifying the void keyword before any methods, we are telling the compiler that this method is not going to return any values. Since we're specifying the void keyword before the main() method in the above simple program, we're telling the compiler that this main( ) method is not going to return any values.
  • main(String args []) - All the methods receives the information from outside the method, by using the variables (also called as parameters) which are specified within the set of parenthesis(i.e. (  ) )that follow the name of the method. Here in the above simple program, we've give the parameter as args[] and we've declared it as String typeargs[] parameter receives any command line arguments present when the program is executed. But this simple program is not making use of String args[], we'll discuss more on this topic in the future posts. So when ever we write the main method, we've to compulsorily use the args[] parameter by declaring it as a String irrespective of whether the main() method will use it or not. For the other methods, when there is noting to be received by the methods, we can keep the braces empty i.e. addition( ). i.e. addition( ) method wont receive any information from outside the method as the braces followed by the method name addition don't have any parameters. 
All the code related to the main() method should be written inside the braces { and } of the main( ) method. The same applies to the other normal methods.


    System.out.println("This is a simple Java program.");


This line of code is written inside the main( ) method in the above simple program. This line prints  This is a simple Java program. output on execution.

We've to understand the 'println( )', 'System' and 'out' terms to understand how this line prints the This is a simple Java program text as output.

  • println( ) - This is a built-in method of Java. i.e. Writing any separate code for this method is not requried, as this is built-in method which is readily available in Java. In this case println( ) method  will display the text that is passed to it under " " as output.
  • System - This is a predefined class of Java that provide access to the system on which the output needs to be displayed. 
  • out - is the output stream that is connected to the console.

Also you may notice that the above line ends with a semicolon ; . All the statements in Java end with a semicolon. All the other lines in the above simple program which don't end with the semicolon are
not statements.

No comments:

Post a Comment