5

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[]){

or

public 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?

2

3 Answers 3

9

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.

2
  • beautifully put.
    – xyz
    Commented Sep 18, 2016 at 17:06
  • also gave me code formatting sources and the like, its really rather perfect answer.
    – xyz
    Commented Sep 18, 2016 at 17:08
1

I dont know about the Books, but if i go by the Oracle Documentation, it mentions there is no difference between both the type of declarations !

The Compiler is happy with either !

4
  • great answer, i think because right now it is a casual level, so that is a good answer thank you
    – xyz
    Commented Sep 18, 2016 at 16:51
  • 2
    Why should someone new to programming favor one style over the other? And if the thought that skill level or adeptness affects the style, how does one determine when to switch? Can you back up these claims with evidence or experience?
    – Thomas Owens
    Commented Sep 18, 2016 at 17:06
  • @Thomas, the opinion is made clearly with backing that its on a casual level , so you can ignore the same. Commented Sep 18, 2016 at 17:30
  • If it hurts the sentiments , i take back my Opinion :-) Commented Sep 18, 2016 at 17:32
0

Since this question is really about how to declare an array variable, I'll answer as such. Either way is fine, in terms of syntax, but I prefer to declare an array like this:

String[] args

This makes it clear that the variable "args" is of type "String[]".

Using the other method makes no logical sense to me...

String args[]

To me, this reads like you are declaring a variable called "args[]" and it is of type "String", which is incorrect. You are still declaring a variable called "args" which is of type "String[]".

1
  • Opinion based. I read the first one like "String array variable called args", the other one as "args is an array variable of Strings".
    – Andy
    Commented Sep 23, 2016 at 10:56

Not the answer you're looking for? Browse other questions tagged or ask your own question.