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

Maroun
  • 91,013
  • 29
  • 181
  • 233
sachin
  • 1,447
  • 4
  • 14
  • 20

3 Answers3

21

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];
}
Peter Lawrey
  • 513,304
  • 74
  • 731
  • 1,106
8

Nothing. The latter is to make C programmers get used to Java :)

See this link, it answers your question.

Important thing to note:

int[] happyArray1, happyArray2;
int happyArray[], happyInt;
Maroun
  • 91,013
  • 29
  • 181
  • 233
1

It's just some "sugar" that Java gives the programmers. It works exactly the same way and is equally efficient. It just makes it easier to learn the syntaxes.

JREN
  • 3,512
  • 3
  • 25
  • 44