Two different ways to declare an array

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

What is the difference between the two?

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

+4
source share
3 answers

The difference is that you have multiple ads. 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[] 

Java uses int[] . The older int a[] should make C programmers happy.;)

Vaguely you can write the following, due to incomprehensible reasons for backward compatibility. Do not do that.

 public int method()[] { return new int[6]; } 
+20
source

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

See the link, it answers your question.

It is important to note:

 int[] happyArray1, happyArray2; int happyArray[], happyInt; 
+8
source

This is just the โ€œsugarโ€ that Java gives programmers. It works the same way and is equally effective. It just makes syntax learning easier.

+1
source

Source: https://habr.com/ru/post/1488015/


All Articles