# Keyword "new" and arrays - clarification

**URL:** <https://discourse.processing.org/t/keyword-new-and-arrays-clarification/22598>\
**Category:** Beginners\
**Created:** [July 13, 2020, 11:13pm UTC](https://discourse.processing.org/t/keyword-new-and-arrays-clarification/22598 "2020-07-13T23:13:06Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![mnoble](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/mnoble/32/99_2.png) [@mnoble](https://discourse.processing.org/u/mnoble)\
**Post date:** [July 13, 2020, 11:13pm UTC](https://discourse.processing.org/t/keyword-new-and-arrays-clarification/22598/1 "2020-07-13T23:13:06Z")

</div>

Hello Forum,

This question is about explaining arrays simply to beginners…please correct or clarify, thank you 🙂

**When an array is declared and initialized at the same time like this:**

**`int mySet[] = {15,75,123,105,165,290};`**

**then key word “new” is unnecessary. However, when an array is declared but not initialized yet then the key word “new” is required like this:**

`int mySet[] = new int[6];`

So, is it fair to say,

**"the keyword “new” is only required when using an array[] that is declared and but not initialized."**

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [July 13, 2020, 11:49pm UTC](https://discourse.processing.org/t/keyword-new-and-arrays-clarification/22598/2 "2020-07-13T23:49:36Z")

</div>

> [@mnoble](#):
>
> `int mySet[] = { 15, 75, 123, 105, 165, 290 };`

Placing the `[]` next to the datatype is more accepted by Java’s conventions:  
`int[] mySet = { 15, 75, 123, 105, 165, 290 };`

> [@mnoble](#):
>
> “the keyword “new” is only required when using an array that is declared and but not initialized.”

Keyword `new` is still required when initialization happens after the variable is already declared:

```auto
int[] mySet;
mySet = new int[] { 15, 75, 123, 105, 165, 290 };
exit();

```

---

<div class="post-metadata">

**Author:** ![mnoble](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/mnoble/32/99_2.png) [@mnoble](https://discourse.processing.org/u/mnoble)\
**Post date:** [July 14, 2020, 12:36am UTC](https://discourse.processing.org/t/keyword-new-and-arrays-clarification/22598/3 "2020-07-14T00:36:32Z")

</div>

" Placing the `[]` next to the datatype is more accepted by Java’s conventions:  
`int[] mySet = { 15, 75, 123, 105, 165, 290 };`"

Does this mean that this array is written incorrectly even though it works in Processing?

```auto
int mySet[] = {15,75,123,105,165,290}; 

void setup (){
  size (300,300);

}

void draw () {
  background (208,236,237);
  noStroke();

   for (int i = 0; i < 6; i++){    
      fill (100,0,100,100);
      ellipse ((mySet[i]),150, 50, 200); //draw ellipses int from the array index       
     }
    }

```

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [July 14, 2020, 12:45am UTC](https://discourse.processing.org/t/keyword-new-and-arrays-clarification/22598/4 "2020-07-14T00:45:42Z")

</div>

> [@mnoble](#):
>
> Does this mean that array is written incorrectly?

Nope! Both the `[]` placement & the `{}` initialization block w/o keyword `new` were introduced on Java in order to attract the C/C++ folks. 😏

---

<div class="post-metadata">

**Author:** ![mnoble](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/mnoble/32/99_2.png) [@mnoble](https://discourse.processing.org/u/mnoble)\
**Post date:** [July 14, 2020, 12:49am UTC](https://discourse.processing.org/t/keyword-new-and-arrays-clarification/22598/5 "2020-07-14T00:49:12Z")

</div>

> [@GoToLoop](#):
>
> Keyword `new` is still required when initialization happens after the variable is already declared:
> 
> ```auto
> int[] mySet;
> mySet = new int[] { 15, 75, 123, 105, 165, 290 };
> exit();
> 
> ```

So, then “new” is only required when using an array that is declared and initialized **in separate lines**?

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [July 14, 2020, 12:52am UTC](https://discourse.processing.org/t/keyword-new-and-arrays-clarification/22598/6 "2020-07-14T00:52:25Z")

</div>

Also when it’s initialized by `length` w/o its content: `int mySet[] = new int[6];`

---

<div class="post-metadata">

**Author:** ![mnoble](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/mnoble/32/99_2.png) [@mnoble](https://discourse.processing.org/u/mnoble)\
**Post date:** [July 14, 2020, 12:57am UTC](https://discourse.processing.org/t/keyword-new-and-arrays-clarification/22598/7 "2020-07-14T00:57:01Z")

</div>

As always - thank you!

---

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [July 14, 2020, 4:28pm UTC](https://discourse.processing.org/t/keyword-new-and-arrays-clarification/22598/8 "2020-07-14T16:28:46Z")

</div>

The nice thing about teaching this format:

```auto
int[] mySet = new int[6];
mySet = new int[] {6, 7, 8, 9, 10, 11};
int[] mySet2 = new int[] {0, 1, 2, 3, 4, 5};

```

…is that the type, int, is used consistently everywhere. Simple, easier to remember, forms good habits.

Then, one an additional rule:

> You may optionally drop the type when initializing during declaration:
> 
> ```auto
> int[] mySet3 = {0, 1, 2, 3, 4, 5};
> 
> ```

---

<div class="post-metadata">

**Author:** ![mnoble](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/mnoble/32/99_2.png) [@mnoble](https://discourse.processing.org/u/mnoble)\
**Post date:** [July 14, 2020, 6:59pm UTC](https://discourse.processing.org/t/keyword-new-and-arrays-clarification/22598/9 "2020-07-14T18:59:29Z")

</div>

Hi Jeremy!

Those are helpful points! I like to keep things consistent. Thank you!
