1

I've coded some in Ruby and C++, but in Java I don't understand what the String[] args parameter to the main method does.

Many simple Java ('hello world') examples are more or less like this:

public class HelloWorldApp {
    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

From what I know about the syntax of other languages args would be used to pass command line arguments to the program. However, these simple examples don't make use of command line arguments.

So, why is the args parameter needed in situations like these when it isn't used?

Matthew Murdoch
  • 30,874
  • 30
  • 96
  • 127
Jones
  • 1,154
  • 1
  • 10
  • 35
  • 1
    _... but that obviously isn't happening._ It is happening. – rgettman Feb 18 '16 at 20:51
  • Oh. Why is it there? What purpose does it serve? – Jones Feb 18 '16 at 20:56
  • You've answered your own question. It's to pass arguments to the program. – Hypino Feb 18 '16 at 20:57
  • 1
    I thought there needed to be code in the program to do something with those arguments. What's the point of having it there if nothing is done with it? – Jones Feb 18 '16 at 21:00
  • They are command-line arguments to your program and they *are* used. You have to configure your IDE to actually use them. By default, `args.length == 0` – OneCricketeer Feb 18 '16 at 21:11
  • 1
    I understand that they are command like arguments. I don't understand why they are there, as far as I can tell they don't do anything. – Jones Feb 18 '16 at 21:12
  • If someone were to run your program using `java HelloWorldApp -x -y -z`, then in your main method args would be an array of three strings, as you would see by executing it in a debugger. The fact that your particular app doesn't do anything with them is irrelevant. – FredK Feb 18 '16 at 22:00
  • So does a java app need to accept arguments? – Jones Feb 18 '16 at 22:07
  • it needs to be able to accept them. your program dosent need input arguments to work,but it does not mean that other programs are just like yours. Other question, why java language allows to do a loops if your program dosent use it? your question is same category... – T.G Feb 21 '16 at 20:16

2 Answers2

1

Because this is not a dynamic language; the number and types of the arguments are part of the method signature, and all the following are different methods:

public static void main(String[] args)
public static void main(Object[] args)
public static void main()
public static void main(Collection<String> args)
public static void main(List<String> args)
public static void main(String[] args, String other)
public static void main(String other, String[] args)

Now, during runtime the proper method will be called that implements the most specific type parameters as determined during compilation. (note: this does not include parameter names).

This has as a side-effect that a main method must have a specific signature that will work for all cases. When you run without arguments, the args array will still be present, but have a zero length. If your program does not accept arguments, you're free to completely ignore the args parameter.

Tassos Bassoukos
  • 16,017
  • 2
  • 36
  • 40
0

The signature of the method that is executed on JVM startup is specified in the Java language specification, §12.4.1:

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.

Thus you always have to add this parameter. Note that you can use String[] or String... as the type.

siegi
  • 5,646
  • 2
  • 30
  • 42