-1
int[] myArray = new int[5];

Hey guys, I know there are other ways to initialize an array like:

int[] myArray = {1,2,3,4,5};

But can you tell me what the second int in the top example stands for? I think the first "int" already specifies the array to have integer values, doesn't it?

Jayendran
  • 9,638
  • 8
  • 60
  • 103
AvoKarato
  • 3
  • 3
  • Yes it specifies that the values are ints but not how many values are allows – Hearner Aug 06 '18 at 14:16
  • defines an array that has space to hold 5 int. You can find further information on this link : https://stackoverflow.com/questions/6222531/why-int-a-new-int1-instead-of-just-int-a – NullPointer Aug 06 '18 at 14:17
  • The first `int` is for declaration: this variable will be an array containing primitive `int` elements. The second `int` is for instantiation: you are allocating memory to hold the `int` elements. – Bucket Aug 06 '18 at 14:17
  • The first `int` is part of the specification of the type of variable `myArray`. The second `int` is part of a Java expression that instantiates an array of `int`. That the expression produces an object of the correct type for initialization of the variable does follow from the repeat of `int`, but that does not imply that it is redundant. – John Bollinger Aug 06 '18 at 14:18
  • @JohnBollinger so `int[] myArray = new [5]` would be wrong right? So the second **int** is like a safety mechanism for java to know that i cant be a byte, short or long value ? – AvoKarato Aug 06 '18 at 14:48
  • 1
    @AvoKarato your proposed alternative is syntactically invalid. The `new` operator requires a type as its operand, and `[5]` is not a type. You can view the requirement for the type to be repeated as a safety mechanism, but I prefer to understand it by viewing the overall statement is a compound of two substantially independent pieces (variable declaration and initializer), each of which has its own rules. Java is requiring *consistency* in how you instantiate arrays. – John Bollinger Aug 06 '18 at 15:00

5 Answers5

2

The construct in int[] myArray = { 1, 2, 3, 4, 5 }; is handled as a special case. The compiler knows that { 1, 2, 3, 4, 5 } is creating an int[] array because of the declared type.

However, the full array creation syntax for the int array is new int[5] (it can vary), which tells the compiler that a new int[] array is created, regardless of the declared type:

For example, you can declare an Object variable, and assign an int array to it:

Object myArrayObject = new int[5]; //OK: this creates an int array

Whereas this won't work:

Object myArrayObject = { 1, 2, 3, 4, 5 }; //won't compile
ernest_k
  • 44,416
  • 5
  • 53
  • 99
  • 1
    And as I recall, the `int[] myArray = { 1, 2, 3, 4, 5 }` form wasn't always supported. I don't remember when it was introduced, but I'm pretty sure that back in the day you had to spell out `int[] myArray = new int[] { 1, 2, 3, 4, 5 }` (which you still can do). – John Bollinger Aug 06 '18 at 14:22
  • But when `int[] my Array = new` **any other type than int** `[5]` wouldnt compile, why does java want me to say **int** AGAIN? – AvoKarato Aug 06 '18 at 14:42
  • 1
    @AvoKarato It's not just about stating `int`. Remember, you also specify the size (`new int[5]` or `new int[arraySize]` using a variable). Would you have wanted java to allow you to type `new [5]` or something like that? It doesn't make much difference and the benefit of allowing developers to omit `int` just when initializing inline would be insignificant. – ernest_k Aug 06 '18 at 14:45
1

So when you type new, you are specifying you want java to allocate memory to the heap. How much memory? Ah yes enough memory for 5 integers, int[5]. So java will allocated enough consecutive memory on the heap to store 5 integers.

hromer
  • 151
  • 1
  • 13
1
int[] myArray = new int[5];

means you declare an array that can contain integer values called myArray (by int[] myArray) and you define it as an integer-array of size 5 afterwards (= new int[5];);

These steps are made inline, you could alternatively do the same in two lines:

int[] myArray;        // declaration
myArray = new int[5]; // definition
deHaar
  • 17,687
  • 10
  • 38
  • 51
1

With int and other primitive types, you're correct, the double declaration is largely superfluous.

But consider this example:

Object [] array = new String[5];

This is valid Java code (even though it is slightly broken*).

On a more general level it follows the same pattern as when you're initialising any variable with a new object:

Foo foo = new Foo();

The first Foo is the type of the variable (what shape the box is), the second is the type of the actual object (what shape is the thing you put in the box).

Note: Starting with Java 10, you can declare local variables with the keyword var, thus eliminating the need to specify the type twice:

var array = new int[5];

The compiler will infer from this that the type of array will be the same as the object it's initialised with. (int[] in this case).

*It is broken because if you try to put into the array anything other than a String, the code only fails at runtime, not during compilation.

biziclop
  • 48,926
  • 12
  • 77
  • 104
0

First is the data type of the array. And other int is for the intialization of 5 integer object in the array

Taha alam
  • 372
  • 3
  • 11