I've seen two methods of declaring an array, such as the String[] args portion of the main method:public static void main(String args[]){orpublic static void main(String[] args){The textbook that I am using has wrote it the first way I described. However, I feel more comfortable writing it the second way.Is there a recommend or conventional standard in the Java language for where to place the square brackets or is this just a personal preference of the developer who is writing or reviewing the code?
When declaring an array in Java, what is the conventional location for the square brackets?
java;coding style
In short: The style guides that I have easy access to support public static void main(String[] args) as the preferred method. However, the language doesn't differentiate between those two options.From a language perspective, both are equally valid. Section 10.2 Array Variables of the Java Language Specification allows for both. In fact, some of the examples even go a step further. The documentation also says that the support for both styles is a nod to the tradition of C and C++.From a standards perspective, the Oracle Code Conventions (which were last updated April 20, 1999) and the Google Java Style Guide. Although I couldn't find it in the text of the Oracle conventions, you can see an example of it in Section 11 - Code Examples of the Oracle Code Conventions. The Google Java Style Guide is more clear - Section 4.8.3.2 No C-style array declarations states that the square brackets form a part of the type, not the variable. However, these are simply two examples - if you looked hard enough, you may be able to find a style guide that states that the square brackets should be on the variable and not the type.Since the language doesn't care, which one should you use? The most important thing is consistency. If you're working on something on your own, be consistent with the code already written. This will make it easier for you and others to understand as you read the code in the future. If you're working on a team, follow the team's style guide. If the code is not consistent across the project, make it as consistent as you can across a class or module.