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?

4

7 Answers 7

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 
Sign up to request clarification or add additional context in comments.

1 Comment

In your demo I recommend validating the length of args first.
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.

2 Comments

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 On your suggestion, I modified the working, hopefully for better clarity.
1

It means that the function expects an array of strings.

Comments

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 

Comments

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 

1 Comment

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.
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

Comments

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.