curly braces when define array curly braces when define array arrays arrays

curly braces when define array


This is just a shortcut code to create an array with initial elements, the followings (which are equal):

    int[] to = new int[] { text };    int[] to = { text };

can be substituted with

    int[] to = new int[1];    to[0] = text;

Hope this helps.


The curly braces contain values to populate the array.


This syntax allows you to define the contents of an array and is often referred to as an array literal.

In this context this can actually be simplified to:

int[] to = { 1, 2, 7, etc. };

Adding new int[] before it is only required when not part of an assignment, something like:

someFunction(new int[]{1, 3, 5});