Passing an array to a Java function

Possible duplicate:
java - array brackets after the variable name

When writing a Java function that takes an array as a parameter, should the function definition have brackets ("[]") for the type or variable name?

How in:

private int myFunction(int array[])
{
    //do stuff here
}

or...

private int myFunction(int[] array)
{
    //do stuff here
}

They both "work", but is there a difference in the technical differences between them?

+5
source share
4 answers

There is no difference. But int[] arrayit is considered idiomatic Java.

The logic is that it []is part of a type (an array is a separate type from a scalar), intand therefore []must live together. It is also consistent with the designation, for example. return types:

int[] foo() {
    ...
    int[] x = new int[5];
    return x;
}
+15

, , ,

int[] array1, array2;

.

int array1[], i;

.

+2

. , . Java- , . int[] array.

+1

int[] array , int[] - , array - . .

0

All Articles