2

I added the following code to a new class I created in Java:

public static void main(String[] arguments) {

I understand what public, static and void mean, but what does (String[] arguments) mean?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Tony Smith
  • 73
  • 1
  • 2
  • 9

7 Answers7

11

Your main() method can take input parameters of type String if your program is run through a console like

java YourClass arg1 arg2

Now, within main() if you iterate the String [] like

for (arg : arguments)
    System.out.println(arg);

it should print

arg1
arg2

Demo :

public class AddTwoNumbers {

    public static void main(String[] args) {
      if(args.length == 2) {
        try {
            int a = Integer.parseInt(args[0]);
            int b = Integer.parseInt(args[1]);
            System.out.println("a + b = " + a + " + " + b + " = "+ (a + b));
        } catch (NumberFormatException e) {
            System.err.println("Invalid Input: Please enter numbers.");
        }
      } else {
         System.err.println("Missing Input: Please enter TWO numbers.");
      }
    }
}

You can run this on your console as

java AddTwoNumbers 2 3

and it should print

a + b = 2 + 3 = 5
Ravi K Thapliyal
  • 51,095
  • 9
  • 76
  • 89
3

It literally means "an array, where every element inside of it is at least a String, which we will name arguments".

In the context of the rest of the line, the method main takes as input, "an array, where every element inside of it is at least a String, which we will name arguments"

Since this is the public static void main(String[] arguments) method call, there is a special "exception to the rule" of normal parameter passing. Methods that look like this are one of the very few times when your input is not defined in some other part of your program. Instead the Java Virtual Machine constructs the input into this method, from the command line arguments you gave the JVM.

Edwin Buck
  • 69,361
  • 7
  • 100
  • 138
  • Know you have it in inverted commas, but still would the word 'exception' in its English language sense when talking about Java, especially to beginners :) – anotherdave Jul 15 '13 at 18:44
  • 1
    @anotherdave On your suggestion, I modified the working, hopefully for better clarity. – Edwin Buck Jul 15 '13 at 18:50
1

It means that the function expects an array of strings.

dataskills
  • 646
  • 7
  • 15
1

String[] arguments is the array for run time argument to your java program. If required you can pass arguemnts to your java program like this:

java yourJavaMainClass args1 args2

In your java code you can use the arguments provided by simply iterating over this array.

arguments[0] // this should give you the args1
Juned Ahsan
  • 67,789
  • 12
  • 98
  • 136
1

These are the parameters that the main function expects.

String [] arguments is a java array of String objects. This means that the main function expects an array of Strings. This array of strings typically holds all command line parameter arguments passed in when the program is run from the command line.

From the command line

 java className stringOne stringTwo

In the program Note : means in .. So read this as for stringObject in arguments

for (stringObject : arguments) {
     System.out.println(stringObject);
}

Or if you know the exact amount of Strings that will be in arguments is two then

System.out.println(arguments[0]);
System.out.println(arguments[1]);

Output->

stringOne

stringTwo

Hope this helps! Good luck learning Java

Paul Renton
  • 2,652
  • 6
  • 25
  • 38
1

It's the array of parameters that you may pass to your program during the execution. Ex:

java YourClass param1 100 X

In your runtime, you'll have this array

System.out.println(args.length);  //prints 3
System.out.println(args[0]); //prints param1
System.out.println(args[1]); //prints 100
System.out.println(args[2]); //prints X
elias
  • 15,010
  • 4
  • 40
  • 65
  • String is class name and args is a variable that can be changed. String [] args means String type array.It actually passing argument to main method. – Md.Rakibuz Sultan Jan 30 '19 at 20:13
0

Run the program using command line and the input provided there gets stored in obj array. So if you run the class as java className a1 a2 then object array will have a1 and a2 as elements.

Or if you are using eclipse IDE the go to run/debug config and do as shown enter image description here

Rupesh
  • 2,627
  • 1
  • 28
  • 42