3

Why declare an array type[] arrayName instead of type arrayName[]

I installed an app called programming hub beta app from google play that discourages declaring an array in this format type arrayName[]. It doesn't give the main reasons why. but it just says conventions discourages it. I would wish to understand why not use type arrayName[] instead of this type[] arrayName. Or may be i am wrong.

1 Answer 1

3

type[] arrayName declares a variable whose type is an array of type. The array notation [] is part of the type of the variable, so it's more consistent to attach it to the type of the variable instead of the variable name.

The type arrayName[] notation is only supported since other languages support it.

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

2 Comments

"Other languages" - specifically C. The (now) discouraged syntax was originally supported because it was thought that it would make Java more acceptable to C programmers. The reason it is supported NOW is backwards compatibility.
from legibility point of view as well, seeing the brackets just after the type name is more significant and obvious to the reader. since both ways are interpreted the same way by the compiler or at least the result is the same to avoid confusion it is safer to stick to one style

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.