//
you're reading...
Java Programming

public static void main(String args[]) – explanation


public static void main(String[] args)

This is definitely not want to come out of thin air, nor dead provision does not make sense, but need java program execution.

before jvm attempting to run a class, it has to check whether the class includes a special method. This method must be public, in order to obtain access to any position. This method must be static, because this method does not rely on any instance of the class to run, rather than the static method, before running the first instance of an object of that class is created.

This method has no return value. And C / C ++ program is not the same, java programs are in default normal end, the main() does not return int. If you want to state very end of the process, you can use System.exit(1).

This method must be able to accept a variable number of parameters of type String, as may be run additional operating parameters. For example

java HelloWorld jack 100

, the 100 and jack is operating parameters.

Why it is String? Because String is universal. Any literal forms may be interpreted as String, while other types is not the case (such as the jack can not be interpreted as an integer or floating point), so with a String parameter to store the most appropriate. And because a limited number of parameters, so use the array, ie String []. Later in Java 1.5, it can also be written as String …, representing the number of uncertain.

As for the argument can be any variable name, as long as the internal method according to the parameter variable name to get on the list, from the perspective of the role of the variables, it is called arguments or args is certainly the most appropriate.

As to why the name of this method must be main(), for historical reasons, because the first C program to function as the main entrance, java follows the history of this tradition.

public Permissions modifier, the greatest authority.
static With MianDemo class loading and loading, it vanishes
void no return value
main function name, jvm identify specific function names
(String [] args) (String [] args): defines a string array parameter

class MainDemo 
{
    public static void main(String[] args) 
    {
        String[] arr = new String[]{"zhang","li","wang","zhao"};
        MainTest.main(arr);
    }
}
 
class MainTest
{
    public static void main(String[] args)
    {
        for (int x=0; x<4; x++)
        {
            System.out.println(args[x]);
        }
         
    }
}

1, run java Main Demo, suggesting cross-border, because jvm pass over an array of length zero, there is no element.

2, but if running java MainDemo zhang li wang zhao, output is zhang.

Because this command, equivalent jvm to main() function is passed a String [] args = new String [] { “zhang”, “li”, “wang”, “zhao”} array.


class MainDemo
{
	public static void main(String[] args)
	{
		System.out.println(args.length);
		System.out.println(args[0]);
	}
}

to sum up:

1, main() function is invoked jvm, jvm to main () to pass a string of 0 array, that is, new String [0]

2, it can pass the value of the operation by the class name .main (), because the main() method is a static.

Advertisements

About daviewning

I am an Oracle DBA

Discussion

No comments yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: