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
  • 94,125
  • 30
  • 188
  • 241
sachin
  • 1,447
  • 4
  • 14
  • 22

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
  • 525,659
  • 79
  • 751
  • 1,130
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
  • 94,125
  • 30
  • 188
  • 241
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,572
  • 3
  • 27
  • 45