0

Let say we have the following two different methods to declare array:

// Method 1
int[] myArray = new int[10];
// Method 2
int myArray[] = new int[10];

What do the following parts do internally when we creating an array?

  1. Part 1:

    int[] myArray
    
  2. Part 2:

    int myArray[]
    
  3. and Part 3:

    new int[10]
    

And what is the importance of [] in case of creating an array? And what is the difference between int[] myArray and int myArray[] method while creating an array?

azro
  • 35,213
  • 7
  • 25
  • 55
Daddy Boy
  • 119
  • 1
  • 6
  • `[]` denotes that it is an `Array` and not just a regular `int`. `int[]` declares a variable that can hold an Array of int's, as does `int myArray[]`. `new int[10]` initializes an `Array` of ten elements – GBlodgett Dec 22 '18 at 20:37
  • 1) `int` and `int[]` are different types. 2) no difference – Andrew Tobilko Dec 22 '18 at 20:38
  • Expressions 1 and 2 are not expressions. – Andy Turner Dec 22 '18 at 20:38
  • Does the `[]` is the sign to the `compiler or interpreter` to create the consecutive memory location for this data? – Daddy Boy Dec 22 '18 at 20:43
  • The answer to your question is here https://stackoverflow.com/questions/14559749/java-int-array-vs-int-array and here https://stackoverflow.com/questions/1200621/how-do-i-declare-and-initialize-an-array-in-java – Stack Overflow Dec 22 '18 at 20:47
  • No, `[]` does not signal to the compiler to create consecutive memory. The expression `new int[10]`, is what allocates new memory for you. – markspace Dec 22 '18 at 21:49

0 Answers0