Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Two different ways of declaring an array [duplicate]

Tags:

java

arrays

int arr[] = new int[6];  
int[] arr = new int[6];

What is difference between them?

If there is no difference then what is the purpose of having two different ways?

like image 408
sachin Avatar asked Nov 28 '22 09:11

sachin


1 Answers

The difference is if you have multiple declarations. Otherwise it is a matter of taste

int[] a, b[]; // a is int[], b is int[][]
int a[], b[]; // a is int[], b is int[]

The int[] is preferred in Java. The older int a[] is to make C programmers happy. ;)

Confusingly you can write the following, due to obscure backward bug compatibility reasons. Don't do it.

public int method()[] {
    return new int[6];
}
like image 84
Peter Lawrey Avatar answered Dec 05 '22 18:12

Peter Lawrey