Tuesday, December 03, 2013

JAVA main method

1. You can have more than one method named main(...), as long as they all have different method signatures.

i.e.  Note that the final main(...) method in the demonstration code below will not compile, as a method's return type does not count as part of its signature.

public class ManyMains {
    public static void main(String[ ] args) {  }

    public static void main(String args) {  }

    public void main(String[ ][ ] args) {  }
    // public static String main(String[  ] args)  { return " ";  }          // will not compile: return type doesnt count in signature
}


2.  Each space in the command line arguments delineates one new element in the args[ ] array of incoming parameters.

i.e. The code below prints five separate parameters for the space-delimited arguments A B C D E but just one parameter for the comma-delimited arguments A,B,C,D,E

public class ShowArgs {
    public static void main(String[ ] args) {
        if ( args.length  != 0 ) {
            for ( int x = 0;  x < args.length;  x++ ) {
                System.out.println( "Parameter: " + args[ x ] );
            }
        }
    }
}

3.  If no command arguments are passed in, then the parameter array commonly called args[ ] still gets generated.  But then args.length = 0.
i.e. This program references args but compiles and runs even when no arguments are supplied.

public class NoArgs {
    public static void main(String[ ] args) {
    if (args.length != 0  &&  args[ 0 ].equals( "Hello" ) ) {
            System.out.print( "Hello  World" );
        }
    }
}

4.  You do not need a main(...) method to compile successfully. However any execution attempt gives a NoSuchMethodError.

5.  You do not need to put public on your main(...) method to have it compile and run.  public is just a convention.  It can be changed to protected, private or "default/package" and it will still work.

6.  If you take out static the method will compile but it will not run.

7.  If you take out void it will not compile.

8.  You can switch the order of the two words public and static.

9.  The main(...)  method can be made final.

10. The odd variations on the main(...) method shown below all work  Reasons are shown in their comments:

protected static void main(String[ ] args)        // public is only a convention, not required
private static void main(String[ ] args)             // public is only a convention, not required
static void main(String[ ] args)                          // public is only a convention, not required
public static final void main(String[ ] args)      // main(...) can be final
static public void main(String[ ] args)              // only void is position-sensitive, not public or static
final static void main(String[ ] args)                  // only void is position-sensitive
public static void main(String parm[ ])             // the identifier args is just a convention
strictfp static void main(String  [ ]array)           // main(...) can be strictfp

No comments:

Post a Comment

Distributed Transactions

What is a distributed transaction?  Transactions that span over multiple physical systems or computers over the network, are simply termed D...