5

I'm trying to print "Hello World" from a Java program, but I am a little confused with the main method:

public static void main(String[] args)

and

public static void main(String args[])

Both these functions perform the same task. How does this happen?

I know the function of String but not of the args.

3
  • 2
    See stackoverflow.com/questions/129178/… Commented Jul 9, 2009 at 12:52
  • I think maybe a book on Java might be in order here? Commented Jul 9, 2009 at 12:57
  • 1
    is the second really with an all lowercase characters "string"? Commented Jul 9, 2009 at 14:48

7 Answers 7

18

In Java:

String args[]

is exactly equivalent to:

String[] args
Sign up to request clarification or add additional context in comments.

1 Comment

You could also use String... args in the method as this is the only argument and its a variable length String array
12

You are defining a method named "main" which is public (anyone can call it), static (it's a class method, not an instance method), and returns void (does not return anything), and takes a parameter named args that is a String array.

In Java you can declare a String array as:

String[] args

or

 String args[]

Comments

3

You can define an array in Java in two equivalent manners (as you written):

  • String[] args
  • String args[]

So it doesn't really matters. The first way is a bit more common, though.

Comments

0

In java your array declaration can be after the type or the name of the variable, so it is ok both methods to perform the same functionality.

args are used to store command line arguments.

Like

java YourClass arg1 arg2

arg1 and arg2 will be stored in args array at indexes args[0] and arg[1].

Comments

0

The args variable is for inputs to your program. See tutorial on command-line arguments.

Comments

0

Im gonna try to explain all the parts of this line public static void main(String[] args)

main: its a Java convention inherited from C.. the first method to run its the one called main.

void: this method should return nothing, which in java its called void

static: you don't need to create an instance of this class to run this method(completely logic if is the first one to run)

public: it is possible to call this method from other classes, even outside of the package(again its logic been the firsts method to be called)

Now to your question String[] args is completely equivalent to String args[] or to String[] name... anyway, it means that args(or name in the last case) its the name of the array of Strings that the method is receiving. Its an array because of the symbols [] and in java is valid to put the symbols in front of the type(String for this case) or in front of the name of the variable(args). If you run your program from a terminal with a command like "java myprogram argument1 argument2" inside the method main the value of args will be args = ["argument1", "argument2"]

Comments

0

There is exactly no difference between the two, though according to the Java standards, the convention for making an array is:

String[] args

rather than,

String args[]

and equally valid form using the ellipses as well (only for infinite array):

String...args

A brief overview of the psvm() in Java:

public: the method can be accessed anywhere within the class or outside the class or even outside the package.

static: the method is shared by all the objects and it does not need any objects to be created to call this method.

void: this particular main() method cannot return any value so here we have to use void; it is the syntax which need to be followed.

main(): this is the entry point of the code or the class you execute. In most cases, this is also the exit point of the code.

String[]args: these are run-time commands line arguments, whether you pass them or not you have to mention them in the arguments.

1 Comment

This does not address the original question, which is the difference between String[] args and String args[].

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.