What is “String args[]”? parameter in main method Java

前端 未结 17 1475
有刺的猬
有刺的猬 2020-11-21 23:58

I\'m just beginning to write programs in Java. What does the following Java code mean?

public static void main(String[] args)
  • What

相关标签:
17条回答
  • 2020-11-22 00:16

    Even tho OP is only talking about the String[] args, i want to give a complete example of the public static void main(String[] args).

    Public : is an Access Modifier, which defines who can access this Method. Public means that this Method will be accessible by any Class(If other Classes are able to access this Class.).

    Static : is a keyword which identifies the class related thing. This means the given Method or variable is not instance related but Class related. It can be accessed without creating the instance of a Class.

    Void : is used to define the Return Type of the Method. It defines what the method can return. Void means the Method will not return any value.

    main: is the name of the Method. This Method name is searched by JVM as a starting point for an application with a particular signature only.

    String[] args : is the parameter to the main Method.

    If you look into JDK source code (jdk-src\j2se\src\share\bin\java.c):

    /* Get the application's main method */
    mainID = (*env)->GetStaticMethodID(env, mainClass, "main",
                       "([Ljava/lang/String;)V");
    ...
    {    /* Make sure the main method is public */
    ...
    mods = (*env)->CallIntMethod(env, obj, mid);
    if ((mods & 1) == 0) { /* if (!Modifier.isPublic(mods)) ... */
        message = "Main method not public.";
        messageDest = JNI_TRUE;
        goto leave;
    ...
    

    You can see that the starting method in java must be named main and must have the specific signature public static void main(String[] args)

    The code also tells us that the public static void main(String[] args) is not fixed, if you change the code in (jdk-src\j2se\src\share\bin\java.c) to another signature, it will work but changing this will give you other possible problems because of the java specs

    Offtopic: It's been 7 years since OP asked this question, my guess is that OP can answer his own question by now.

    0 讨论(0)
  • 2020-11-22 00:16

    String[] args means an array of sequence of characters (Strings) that are passed to the "main" function. This happens when a program is executed.

    Example when you execute a Java program via the command line:

    java MyProgram This is just a test

    Therefore, the array will store: ["This", "is", "just", "a", "test"]

    0 讨论(0)
  • 2020-11-22 00:17

    Those are for command-line arguments in Java.

    In other words, if you run

    java MyProgram one two

    Then args contains:

    [ "one", "two" ]

    public static void main(String [] args) {
        String one = args[0]; //=="one"
        String two = args[1]; //=="two"
    }
    

    The reason for this is to configure your application to run a particular way or provide it with some piece of information it needs.


    If you are new to Java, I highly recommend reading through the official Oracle's JavaTM Tutorials.

    0 讨论(0)
  • 2020-11-22 00:19

    Explanation in simple layman's language.

    The main method expects us to provide some arguments when we direct our JVM to the class name. That means, suppose your file name is Try.java, now to execute this in command prompt you write "javac Try.java" to compile followed by "java Try" to execute. Now suppose instead of writing simply "java Try" you write "java Try 1". Here you have passed an argument "1". This will be taken by your main method even if you don't use it in your code.

    If you want to check whether your main method has actually taken the argument "1" or not. Simply, inside your main method type the following:

    for(int i = 0; i < args.length; i++) {
            System.out.println("Argument is: "+args[i]);
        }
    
    0 讨论(0)
  • String [] args is also how you declare an array of Strings in Java.

    In this method signature, the array args will be filled with values when the method is called (as the other examples here show). Since you're learning though, it's worth understanding that this args array is just like if you created one yourself in a method, as in this:

    public void foo() {
        String [] args = new String[2];
        args[0] = "hello";
        args[1] = "every";
    
        System.out.println("Output: " + args[0] + args[1]);
    
        // etc... the usage of 'args' here and in the main method is identical
    }
    
    0 讨论(0)
  • 2020-11-22 00:22

    in public static void main(String args[]) args is an array of console line argument whose data type is String. in this array, you can store various string arguments by invoking them at the command line as shown below: java myProgram Shaan Royal then Shaan and Royal will be stored in the array as arg[0]="Shaan"; arg[1]="Royal"; you can do this manually also inside the program, when you don't call them at the command line.

    0 讨论(0)
提交回复
热议问题