Why Does Main Method in Java Always Need Arguments

Why does main method in Java always need arguments?

Basically, there are four answers:

  1. Because that's the way it was designed. Yea, I know that's a circular reason. But the point is that this is the way it is and it ain't going to change. So unless you are planning on designing your own language, the question is moot.

  2. Cleanness of design (aka the DRY principle). Don't specify two entry point signatures when one can do the job. And clearly, it can.

  3. Semantic simplicity. Suppose (hypothetically) that Java did support both void main(String[]) and void main() entry points. What would happen if a class defined both methods? Is that an error? If not, which one takes precedence when there is ambiguity? Is this confusing yet?

    By only recognizing void main(String[]), the JLS avoids that problem1.

  4. This is analogous to the standard C and C++ entrypoint signatures. (Admittedly, some C / C++ runtimes support other non-standard entrypoints as well, but that's not exactly a good thing2.)

None of this means that it would have been unambiguously wrong to do it another way. And for example C# gives you alternative signatures, and deals with the ambiguity problem by requiring the developer to designate an entry point some other way.

FWIW, this wikipedia page describes the "main" method in a number of languages.


1 - Though then you have the "problem" that people who are new to Java might guess (incorrectly) that multiple entry points ought to work, try it, and get a surprise. But I don't think any design could cope with "programming by guesswork".

2 - For a start, it is a portability issue.

Why is it necessary to have `String[] args` as main() parameter?

From Java Language Specification 12.1.4

The method main must be declared public, static, and void. It must specify a formal parameter (§8.4.1) whose declared type is array of String. 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();
}

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 JAVA requires String[] parameter in main function even if we do not want to provide command line arguments?

It's the Java specification for main method.

The method main must be declared public, static, and void. It must specify a formal parameter (§8.4.1) whose declared type is array of String.

Ref: 12.1.4. Invoke Test.main

Why does Java main() method accept an array of String args?

even a gui driven java app will start with some main method.
The "higher purpose" has never been to accept command line arguments.

The purpose is just to accept arguments. Period. Whenever you start any program not just Java you will always need some syntax to pass arguments

How JVM can call main() method without argument . No Error Why?

Who says the main() method is called without any argument? An empty String array is passed to adhere to the method signature. If you pass any other arguments to the program, then they are added to this array.

public static void main(String[] args) {
System.out.println(args);
System.out.println(args.length);
}

O/P :

[Ljava.lang.String;@5d888759
0

What is the purpose of specifying the type of argument a main method takes in?

Does the type of argument of a main method matter?

Yes. The JVM uses public static main(String[] args) as the entry point of execution for general Java applications. In other words, this is where the JVM is going to hand off execution from its own internal loading and initialization routines to your own bytecode. Unless you are executing an application inside a container (eg. GlassFish) or framework (eg. JavaFX) you will need to have this entry point for your code. By convention it is public static main(String...).

Are there situations in which one would explicitly call the main
method with arguments? If so, what is an example of such a situation?

Arguments to main(...) come mainly from two places:

  • The Command line. When you start a Java application from the command line inside a shell or Windows cmd.exe, you can type in some additional data after the name of the JAR file to execute. These additional data are processed into an array of Strings that is passed to your main() method.

  • A configuration setting. Most IDE's allow you to specify command line parameters for a project. You can pass parameters to the entry point (when you build and run your project) in this setting just as you would on the command line.

As mentioned by Dave, your own code would not invoke the main(...) method itself. It really is intended to just be the entry point.

What are the input arguments taken by my main method?

You can always choose to run your program with or without arguments. If you do not specify any arguments, args will be an empty array (as you've demonstrated here). If you do specify arguments, args will contain the list of arguments that were passed.

See Java's documentation on Command-Line Arguments for more information.



Related Topics



Leave a reply



Submit