-5

In java main method, what's the purpose of String args[] in

public static void main (String args[])  ?
Bentaye
  • 9,403
  • 5
  • 32
  • 45
Sachin Rana
  • 381
  • 1
  • 7
  • 17
  • 2
    Did you try it? What happened? – Mat Nov 15 '14 at 12:14
  • 2
    For what would that be useful? – Seelenvirtuose Nov 15 '14 at 12:14
  • 2
    do you have two mouths to eat :), why you not ask for the reason of the 2nd argument yet you have not used the first one and you are asking for others :) – Muhammad Nov 15 '14 at 12:17
  • @Muhammad - your comment makes me laugh :) anyway its not possible I guess coz I really tried it and the compiler is always looking for the main method with parameter of main(String args[]) – Andoy Abarquez Jul 15 '15 at 10:07
  • by the way I am searching for on how to create a personal command-line parameters when running java. ( ex. java MyProject -uname myuser -startDate 2/12/2015 ) so basically the i just want to put a label of the parameter. – Andoy Abarquez Jul 15 '15 at 10:08

4 Answers4

1

args[] is an String array. So you can pass more than one String to your method:

args[0] = "Hello";
args[1] = "World";
...
Wavemaster
  • 1,794
  • 16
  • 27
  • That explains the `...` part. But the parameter itself is there because this is the interface by which arguments get passed to the application from outside when calling `java foobar Hello World` – Neuron Apr 15 '18 at 23:25
1

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.

Neuron
  • 5,141
  • 5
  • 38
  • 59
mgilsn
  • 145
  • 6
0

When you run your program from the command line or specify program arguments in your IDE, those arguments are split by spaces ("hello world" becomes "hello" and "world", for example) and given to your program in the first argument. Even if the other array existed, there would be no use for it.

James Westman
  • 2,680
  • 1
  • 15
  • 20
0

You are questioning why Java (and at the same time C/C++) Main threads have only two arguments. Java class has a requirement to use the main method defined in the main class. You would see an error otherwise:

 "The program compiled successfully, but main class was not found.
  Main class should contain method: public static void main (String[] args)."

This is just a Java requirement you need to adhere to (under silent protest if you wish).

As opposed to that, in C/C++/C++11 you need to have an int main method, but need not have any arugment. i.e. the following is allowed by the C++ main class requirement:

#include <iostream>

int main(void) { return 0; }

Note that in C/C++, you can also pass an integer as argument in the main.

#include <iostream>
int main(int argc){ return 0;}

To summarise, C/C++/Java each have got their own standards which you have to adhere to. You can argue that something isn't right, but as long as you are one of those people who are in the standards committe, I don't think you can change anything much.

A question regarding C main has been answered here. Also, the necessity of the main method with String[] args has probably been explained here already.

Community
  • 1
  • 1
ha9u63a7
  • 6,233
  • 16
  • 73
  • 108