What is the difference between String[] args and String args[]?

Asked

Viewed 1,267 times

17

What is the difference between the statements String[] args and String args[] in Java?

  • Only the position of the brackets, which in the case of vector, can be positioned in these two ways that the compiler will understand in the same way, but it is recommended to keep it close to the type and not the name of the vector. And this is not exclusive to the main method, it is allowed in every vector statement. the method changes nothing, remains the main method that receives a string vector in both forms.

  • 4

    @diegofm no, not at all duplicate, are subjects completely different, the only thing in common is that the example used is in the parameter of Main() which is circumstantial to the question.

  • Isaac, I edited your question because you seem to want to understand the difference between the two statements and not about methods. If that was not your intention, you can use the comment field to let me know that I revert the question to the initial state.

  • @jbueno, you are correct in your edition. I would really like to know the difference between the statements.

  • 1

    @Renan I can’t believe it either. Didn’t you read the other question?

2 answers

20


None. It is only a permissiveness of the language to write in both ways. A (String args[]) is to facilitate for those coming from the C or C++ and is accustomed thus, the other (String[] args) is more intuitive, since the two parts of the variable type statement are together. The first is weird because one part of the type is in one place and the other part is next to the variable.

I consider that the "new" form of Java, which is preferred, is a mistake as well, but it is what you have. The correct one would be []String. Thus becomes a "array of String", just like you have a ArrayList<String>, so it has a "ArrayList of String". String[] you have to read backwards or read strangely: "String in a array".

The original question was about the use in main(). Actually, it’s being used in main(), but this has to do with declaring variables (parameter are no longer variables. If you want to know something about the main(), has a question on the subject. The two forms can be used equally in all situations, there are no cases where only one can be used, not even in the simultaneous declaration of several variables in the same statement, which is not usually very suitable in this case.

  • 2

    I still have no knowledge in collections but by the explanation you have exposed I have managed to understand the difference or in the case the flexibility of language. Thank you so much for the @bigown explanation.

  • O correto seria []String. Assim fica um "array de String" this if you think in Portuguese, if you think in English the right one is "String array", and consequently it makes more sense the String[]

  • @Math no, even in English the correct is array of strings. There’s even language that uses the term Of p/ indicate this, type Array<Of String> or Array Of String. Just as the right thing is *char and not char* as used. Because it is "pointer to character(s)". And in fact it has language uses pointer like ^char. Language is my thing, I’ve thought a lot about it :)

  • 1

    Complementary curiosity, from Java 5 we also have Varargs, (String... args), inclusive, public static void main(String... args) is valid and functional syntax.

10

The execution works both ways without any real difference. The recommendation is to use the syntax String[] args which is more consistent with the Java typing declaration.

The first also allows the creation of several arrays simultaneously, facilitating the use:

String[] array1, array2;
  • 3

    But the question is what the difference is. It’s not a recommendation request.

  • @jbueno changed the order to be clear

Browser other questions tagged

You are not signed in. Login or sign up in order to post.