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.

7 comments:

  1. it is not necessary that the class wch contains main() must be public machi...
    public class temp {
    void print()
    {
    System.out.println("hai in class temp");
    }
    }

    class psvm_test
    {
    public static void main(String arg[])
    {
    System.out.println("hai in main()");
    temp obj=new temp();
    obj.print();
    }
    }


    This program runs.... ;)
    check out... :)

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. also it is not necessary that the class name and file name must be the same... :D u can have different name. But the thing is while compiling, u must use the file name and while running, u must use the class file which has main()

    Also i wonder how u can write main() inside the class in C++ :D :D :D i didnt get it machi...

    ReplyDelete
  4. Hey Harisun... I'm having a doubt.. Are you sure that main() should be INSIDE a class in C++...? Isn't it supposed to be outside..?

    ReplyDelete
  5. Nevermind..didn't notice it was public.. :P

    ReplyDelete
    Replies
    1. opps that was a mistake.....was in java for a whil so by mistake i typed it!!!!!!!! will correct it

      Delete