3

Is there a difference between String[] args and String[] arg?

My assignment is to misspell certain things in a HelloWorld program.

When I misspelled args as arg in:

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

The program compiled and ran correctly. So I'm trying to understand why arg worked just as well as args.

Is arg just an alternate spelling for args, or is there a difference between the two?

EDIT: I just tried misspelling args as ars, and the program compiled and ran correctly again. I'm definitely confused now.

6
  • 8
    Because it's a variable name, you can call it maroun as well (very recommended). Commented Apr 8, 2015 at 14:12
  • Yes, it's only spelling. 'args' is (by convention) the variable name for command-line arguments variable for java. Commented Apr 8, 2015 at 14:13
  • "I'm definitely confused now" - You shouldn't be, you edited the question after you saw the answers... As already mentioned, the variable's name is up to you, the type is important and Java expect the main's parameter to be of type String[]. Commented Apr 8, 2015 at 14:18
  • You can name variables whatever you want, and method parameters are no different (with a few exceptions of course). The answers provided I think clearly show this. The String[] parameter being called args is nothing more than a standard. Commented Apr 8, 2015 at 14:19
  • Thank you everyone for your VERY QUICK responses :) Commented Apr 8, 2015 at 14:20

7 Answers 7

6

There is no difference. Java distinguishes types, variables names are just for you

Sign up to request clarification or add additional context in comments.

Comments

6

As long as you are not using the variable, whether you specified it as args or arg makes no difference. The bytecode doesn't store the type actual name. It knows that there is a variable assigned that accepts a String[].

Comments

1

the arg and args are the variable names for the type you have declared.

These variables name can be anything, though there are some restrictions over it.

You can refer to here for help in setting variable names.

Comments

1

You can call it what ever you want. It is the name of your argument. What really matters is the signature of the method.

When a java class is executed from the console, the main method is what is called. In order for this to happen, the definition of this main method must be

public static void main(String [])

Naming parameter as args is a standard convention, but not strictly required.

In Java, args contains the supplied command-line arguments as an array of String objects.

In other words, if you run your program as

java HelloWorld one two 

then args will contain

["one", "two"]

Comments

0

No. There is no difference. The arg syntax is there mainly for compatibility with C syntax

Comments

0

There is no difference. Variable names are important to you and, in general, the compiler only cares if the variable exists.

Now, about the main method, the following signatures are valid and equivalent:

public static void main(String[] args);
public static void main(String args[]);
public static void main(String... args);

However, javap output is different, the var-args is preserved:

String[]
Compiled from "HelloWorld.java"
public class HelloWorld {
  public HelloWorld();
  public static void main(java.lang.String[]);
}

Notice the difference:

String...
Compiled from "HelloWorld.java"
public class HelloWorld {
  public HelloWorld();
  public static void main(java.lang.String...);
}

Comments

0

While other answers made it clear, and since I cannot write this in a comment, I'll make it an answer.

Maybe a little explanation about the main method signature will help you to understand what is important for Java:

... You can name the argument anything you want, but most programmers choose "args" or "argv".

... This array is the mechanism through which the runtime system passes information to your application.

  • main is declared void because once it finishes, it doesn't mean that the program finished. If it spawns a new thread it might be that these threads are still running.

  • It is public because it is called by the JVM to run the method which is outside the scope of project.

  • ... and is static because when the JVM calls it, there is no object existing for the class being called. So it has to have static method to allow this from class.

1 Comment

You should add the relevant JLS part about main method, this way your answer will be complete :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.