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

1,117,138

Solution 1

In Java args contains the supplied command-line arguments as an array of String objects.

In other words, if you run your program in your terminal as :

C:/ java MyProgram one two

then args will contain ["one", "two"].

If you wanted to output the contents of args, you can just loop through them like this...

public class ArgumentExample {
    public static void main(String[] args) {
        for(int i = 0; i < args.length; i++) {
            System.out.println(args[i]);
        }
    }
}

The program will print in the terminal:

C:/ java MyProgram one two
one
two
    
C:/

Solution 2

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 Java™ Tutorials.

Solution 3

args contains the command-line arguments passed to the Java program upon invocation. For example, if I invoke the program like so:

$ java MyProg -f file.txt

Then args will be an array containing the strings "-f" and "file.txt".

Solution 4

The following answer is based my understanding & some test.

What is String[] args ?

Ans:

String[] -> As we know this is a simple String array.

args -> is the name of an array it can be anything (e.g. a, ar, argument, param, parameter) no issues with compiler & executed & I tested as well.

E.g:

  1. public static void main(String[] argument)

  2. public static void main(String[] parameter)

When would you use these args?

Ans->

The main function is designed very intelligently by developers. Actual thinking is very deep. Which is basically developed under consideration of C & C++ based on Command line argument but nowadays nobody uses it more.

1- User can enter any type of data from the command line can be Number or String & necessary to accept it by the compiler which datatype we should have to use? see the thing 2

2- String is the datatype which supports all of the primitive datatypes like int, long, float, double, byte, shot, char in Java. You can easily parse it in any primitive datatype.

E.g: The following program is compiled & executed & I tested as well.

If input is -> 1 1

// one class needs to have a main() method
public class HelloWorld
{
  // arguments are passed using the text field below this editor
  public static void main(String[] parameter)
  {    
System.out.println(parameter[0] + parameter[1]); // Output is 11

//Comment out below code in case of String
    System.out.println(Integer.parseInt(parameter[0]) + Integer.parseInt(parameter[1])); //Output is 2
    System.out.println(Float.parseFloat(parameter[0]) + Float.parseFloat(parameter[1])); //Output is 2.0    
    System.out.println(Long.parseLong(parameter[0]) + Long.parseLong(parameter[1])); //Output is 2    
    System.out.println(Double.parseDouble(parameter[0]) + Double.parseDouble(parameter[1])); //Output is 2.0    

  }
}

Solution 5

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.

Share:
1,117,138
Ashwin J
Author by

Ashwin J

Updated on July 08, 2022

Comments

  • Ashwin J
    Ashwin J about 2 years

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

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

    • When would you use these args?

    Source code and/or examples are preferred over abstract explanations