4

I am new in programming with Java and I am confused about the following two statements:

public static void main(String args[])

and

public static void main(String[] args)

Are they same? If not, how are they different from each other?

5
  • No difference, although IMO the latter is preferred. Commented Dec 22, 2011 at 15:32
  • The only difference is the place where the brackets are Commented Dec 22, 2011 at 15:32
  • 2
    The real fun begins when you using multidimensional arrays String[] strings[] = new String[1][1]; is really nice. Commented Dec 22, 2011 at 15:39
  • 1
    @larlin only if one is a sociopath O:-) Commented Dec 22, 2011 at 16:09
  • @mcfinnigan Now I got some inspiration from the answer bellow. New improved sociopath java array: String[] strings[], stringss[][] = new String[1][1][1]; Isn't the world great? Commented Dec 22, 2011 at 16:18

6 Answers 6

5

There is no semantic difference between the two forms, the only difference is stylistic.

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

Comments

3

They mean the same thing. The second form is generally preferred, as it puts the array declaration with the type declaration. By the way, there's nothing special about this appearing in the main() method, arrays can be declared both ways any place in your code.

Comments

3

While it's true for single statements there IS a difference in case you define more than one variable:

String[] foo1, foo2; // both variables are of type String[]
String bar1[], bar2; // here they're not. But you really shouldn't do this, causes
                     // unnecessary confusion

Comments

1

Yes, they are the same, yet the convention is to write String[] args as String[] is the type.

Comments

1

Both have exactly the same meaning. However, the first is unconventional and should not be used, because it splits type information. It's a holdover from C.

Comments

1

Its also basically the same as

public static void main(String... args)

which is what I prefer.

Comments

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.