1

I'm playing around with 2-Dimensional arrays in Java a bit and i just cant seem to find a difference between those two ways of initializing a 2-Dimensional Array.

//Option one
int field[][] = new int[n][n];

//Option two
int[][] field = new int[n][n];
Mureinik
  • 297,002
  • 52
  • 306
  • 350
Dena
  • 21
  • 3

2 Answers2

4

They are both legal ways to declare arrays, and carry the same meaning. However, the first form is discouraged, since, as Java's tutorial states it: "However, convention discourages this form; the brackets identify the array type and should appear with the type designation.".

Mureinik
  • 297,002
  • 52
  • 306
  • 350
  • 1
    Plus it may raises a linter alert. Sonar will tell you that it's a C form declaration for instance. – Romano May 17 '19 at 20:06
0

You can't find a difference because it's the same. Java allows you to place the brackets after the variable type or name, it's the same.

Although, by convention, it's better to place after the type and not the name (so it's easier to see the that this is an array)

source: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html

Nir Levy
  • 12,750
  • 3
  • 21
  • 38