What does (String[] arguments) mean?
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
purpose of String args[] in main method in java
The main method has only one because it's a form for standardisation. Oracle does not know how many arguments a programmer will need and what types of them. For this reason, with the args[]
of type String
you can pass N
arguments to your program. You can then parse it to any primitive type in Java.
Here is an example of passing arguments to an application with Java:
java MyApp arg1 arg2 arg3 arg4 ... argN
Every value passed is separated by spaces and based in the position, you can retrieve and manipulate them, for example, if you need the arg at position 4 and convert it to a double
, you can do this:
String toDouble = args[4];
double numericalValue = Double.parseDouble(toDouble);
Also, this form was thought to pass some parameters to define and configure some behaviour of your application, so with an unique array this can be accomplished.
Does args have to be included in the main method?
Yes the args variable is just convention. The variable name can be changed, but it is convention and you would be looked down upon. Source here.
Why is it necessary to have `String[] args` as main() parameter?
From Java Language Specification 12.1.4
The method
main
must be declaredpublic
,static
, andvoid
. It must specify a formal parameter (§8.4.1) whose declared type is array ofString
. Therefore, either of the following declarations is acceptable:
public static void main(String[] args)
public static void main(String... args)
(note that you can't have two main
methods with String[]
and String...
in same class, since varargs are simply syntactic sugar which at compilation time will be replaced with arrays so you would end up with two methods handing String[]
and one class can't have two methods with same name and parameters)
So when you execute command like
java YourClass foo bar
Java Virtual Machine will place foo
and bar
parameters in String[]
array and will try to pass that array to main
method which can accept it as parameter.
This method is also used when command doesn't have any arguments like
java YourType
This decision simplifies our life because we don't need to focus on handling cases where there are two entry points
- one for command with arguments
- and one where command doesn't have any arguments.
We can simply allow user to pass arguments but if we don't wan to handle them we can simply ignore them.
Also remember that we are allowed to have in our class any method which has proper declaration (and doesn't violate any rules inherited from superclass like widening member visibility - we can't make protected method public), so there is nothing wrong with having
public static void main(){
/*your code*/
}
But you need to realize that this method can't be used as entry point, so if you want to start your application from this method you will need to create proper main
method which will execute your main()
method:
public static void main(String ...){
main();
}
Can main function take a String instead of String[]
Answer to your question: NO
Details:
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/java.html
The java command starts a Java application. It does this by starting a Java runtime environment, loading a specified class, and calling that class's main method.
The method must be declared public and static, it must not return any value, and it must accept a String array as a parameter. The method declaration has the following form:
public static void main(String[] args)
Related Topics
Capturing Stdout When Calling Runtime.Exec
How to Capitalize the First Character of Each Word in a String
Difference Between Inheritance and Composition
Swing Animation Running Extremely Slow
How to Save a String to a Text File Using Java
Finalize() Called on Strongly Reachable Objects in Java 8
How to Get the Cellrow When There Is an Itemevent in the Jcombobox Within the Cell
What Causes Javac to Issue the "Uses Unchecked or Unsafe Operations" Warning
How to Convert from Int to String
What's the Reason I Can't Create Generic Array Types in Java
How to Turn a List of Lists into a List in Java 8
Resizing Issue With Canvas Within Jscrollpane Within Jsplitpane