What is the correct Java main() method parameters syntax?

前端 未结 9 1280
旧巷少年郎
旧巷少年郎 2020-12-06 07:56

Is there a functional difference between these methods?

public static void main(String[] args) { }

public static void main(String args[]) { }
相关标签:
9条回答
  • 2020-12-06 08:51

    The other answers are correct in that it doesn't make a difference. Let me just add two further points:

    String ... args is also valid now and in this case again makes no difference.

    The different options to place your brackets do have a consequence, when you define multiple variables. In the following example the variable a is not a String array, but b is one and c is an array of arrays of Strings.

    String a, b[], c[][];

    However, I have to suggest not to use this style for your code, as it can quickly become very confusing. For example, String [] b, c[]; means the same for b and c as above, but especially for c this is non-obvious.

    0 讨论(0)
  • 2020-12-06 08:51

    No, they have no difference. Though... I used to use the second way, until my girlfriend threatened to break if I continued doing it (not kidding). So now I prefer the first way, and I think it looks much better.

    0 讨论(0)
  • 2020-12-06 08:51

    No, the above are equivalent. Arrays in Java can be declared in one of two ways, either:

    String[] myarray;
    

    or

    String myarray[];
    
    0 讨论(0)
提交回复
热议问题