1

The statement

int[] a

is the same as

int a[]

However, what happens when you have multiple declarations in a single line?

What is the difference between

int[] a, b, c

and

int a[], b, c

Does the first one declare three arrays, and in the second they are ints, but a is an array?

EDIT:

Why does the declaration happen that way?

Is this a good reason to use the syntax int[] a rather than int a[]?

9
  • 4
    Does the first one declare three arrays, and in the second they are ints, but a is an array? Did you try it? Commented Nov 13, 2013 at 21:52
  • 2
    @Cruncher I don't think this is an appropriate comment here. The question is legitimate and trying it doesn't really explains why it is so and how such declarations work. Commented Nov 13, 2013 at 21:54
  • 2
    @sashkello the questions asks if it is so, not why it is so. Commented Nov 13, 2013 at 21:55
  • 1
    @Cruncher Sorry, I'll update my question to reflect the intent better. Commented Nov 13, 2013 at 21:57
  • 2
    As I understand it, the first declares three arrays while the second declares an array and two ints. Because there be dragons, I never use comma-separated declarations but instead put everything on separate lines. Commented Nov 13, 2013 at 21:57

6 Answers 6

4

from the JLS Chapter 10.2:

The [] may appear as part of the type at the beginning of the declaration, or as part of the declarator for a particular variable, or both.

For example:

byte[] rowvector, colvector, matrix[];

This declaration is equivalent to:

byte rowvector[], colvector[], matrix[][];
Sign up to request clarification or add additional context in comments.

4 Comments

But they're not equivalent to byte rowvector[], colvector, matrix[]'
True, but to me, that seems to follow logically from the example shown.
"Logic" is a tricky thing. In your example, yes, but it's not so obvious with int a[], b, c;
Essentially, that is the same example, but I suppose it is less obvious. I like @quazzieclodo's answer. It makes it very clear.
2

The other answers cover the what pretty well, but I will try to explain why that happens. Basically, the compiler interprets

XXXXXX a, b, c;

as

XXXXXX a;
XXXXXX b;
XXXXXX c;

no matter what XXXXXX is. So in your examples,

int[] a, b, c;

turns into

int[] a;
int[] b;
int[] c;

and

int a[], b, c;

turns into

int a[];
int b;
int c;

Comments

2

The first one declares three arrays, the second one a single array and two ints. (see Java Language Specification for Java 7, §10.2)

Comments

2
int[] a, b, c

will create 3 arrays named a, b, and c.

int a[], b, c

will create an array of integers in a, and two single integer variables b, and c.

Comments

1
static int[] a, b, c;
public static void main(String[] args){
    System.out.println(a + ", " + b + ", " + c);
}

Output: null, null, null

using static int a[], b, c;

Output: null, 0, 0

Comments

0
int a, b, c, d, e = 4;

is declaring 5 ints but only initialising 'e'.

In the same way,

int[] a, b, c, d, e = new int[4];

will only initialise e.

You'd need something like

int[] a=new int[4], b=new int[4], etc...

which frankly, isn't worth one-lining...

Comments

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.