Sunday, 30 September 2012

EVOLVING FROM C++ TO JAVA-1

TUTORIAL -1

In this tutorial, i will be saying how to transform from c++ programming to java programming.Since this is the first tutorial,I WILL BE EXPLAINING ABOUT JAVA FILE NAMING AND MAIN SECTION.

JAVA FILE NAMING:

In c++ we can name the program file as we wish.But in java WE MUST NAME THE PROGRAM FILE WITH THE NAME OF THE CLASS THAT CONTAINS THE MAIN FUNCTION or else WHILE RUNNING WE MUST RUN WITH THE CLASS NAME THAT CONTAINING THE MAIN FUNCTION.This helps the compiler to understand that there is a class in the name of the file and it contains main function.The compiler will start reading from that class,staring it from the main function.And another thing we must care about it is, There can be ONLY ONE PUBLIC CLASS FUNCTION IN A JAVA PROGRAM.We cannot have more than one public class in java. 

For example:

A c++ program:

public class A
{
       .......
       ........
}
public class B //more than one class can be public
{
     ....................
     ....................
}

 public void main()// the main function can be out of the class
        {
             ..................
             .................
             .................
         }
FILE NAME: example.cpp

A java program:

public class A   //must name the file name with this class name
{
     public static void main(String args[]) // main function must be inside the class
     {
        ...............
        ...............
        ...............
      }
}
class B    //only one class can be public
{
    ................
    ................
}
FILE NAME: A.java

JAVA MAIN FUNCTION:

Syntax: public static void main(String args[])

We must declare THE MAIN FUNCTION AS STATIC because it helps the compiler to call the main function without any object.A STATIC MAIN FUNCTION DOESN'T NEED A OBJECT TO BE CALLED.TheString args[] parameter helps to pass the value from command line.The value passed are stored in string args array.We must declare the main function as public too.