-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?

6
  • Yes it specifies that the values are ints but not how many values are allows Commented Aug 6, 2018 at 14:16
  • defines an array that has space to hold 5 int. You can find further information on this link : stackoverflow.com/questions/6222531/… Commented Aug 6, 2018 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. Commented Aug 6, 2018 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. Commented Aug 6, 2018 at 14:18
  • 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. Commented Aug 6, 2018 at 15:00

5 Answers 5

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
Sign up to request clarification or add additional context in comments.

3 Comments

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).
But when int[] my Array = new any other type than int [5] wouldnt compile, why does java want me to say int AGAIN?
@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.
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.

Comments

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.

Comments

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.