04.04.2013 Views

Processing: Creative Coding and Computational Art

Processing: Creative Coding and Computational Art

Processing: Creative Coding and Computational Art

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

PROCESSING: CREATIVE CODING AND COMPUTATIONAL ART<br />

84<br />

system, which I’ll discuss shortly. Arrays are a little difficult to get your head around at first,<br />

but they are absolutely critical to coding, <strong>and</strong> you’ll be using them lots throughout the<br />

book. So if some of this explanation feels a bit abstract, don’t worry—you’ll be revisiting<br />

these concepts again <strong>and</strong> again.<br />

Arrays don’t have a default data type, but rather are declared with a data type just like<br />

variables are. Following is an example of an array declaration:<br />

int[] xpos;<br />

The declaration is done the way you’d declare a regular int variable, with the addition of<br />

the two brackets. Here are two more array declarations:<br />

float[] xspeed;<br />

String[] names;<br />

You see the pattern—the type comes first, then the brackets <strong>and</strong> then the identifier or<br />

name of the array. You’ll notice in the two declaration lines that I wrote “float” in all lowercase,<br />

but “String” with an initial cap. This was not an arbitrary decision on my part; float<br />

is a primitive data type <strong>and</strong> needs to be all lowercase. String is the name of a class, <strong>and</strong><br />

the convention is to name a class with an initial cap. I’ll be covering classes later in the<br />

book, but for now you can think of a class as representing a unique data type—which you<br />

can use when you declare a variable, or in this case an array.<br />

Arrays can be any size (actually as large as the int data type, which is big enough) <strong>and</strong><br />

hold any legal type. However, once they are declared, their type cannot change, <strong>and</strong> once<br />

they are initialized, their size cannot change. Therefore, if I declare<br />

float[] xspeed;<br />

then the xspeed array can now only hold values of type float (real numbers). Here’s how<br />

to initialize the xspeed array:<br />

xspeed = new float[100];<br />

The keyword new reserves space in memory for the array. You‘ll be using the keyword new<br />

a lot more when we get to OOP.<br />

Now the xspeed array has 100 places in memory reserved for float values. Its size, like its<br />

data type, is now immutable, meaning that it can’t be changed. The array now has space in<br />

the computer’s memory reserved for it, although it still doesn’t have any specific values<br />

assigned to those 100 places. The two lines can be put together into one declaration <strong>and</strong><br />

initialization statement:<br />

float[] xspeed = new float[100];<br />

Here are a few more examples of declaring <strong>and</strong> initializing arrays:<br />

int[] items = new int[50];<br />

String[] itemNames = new String[10000];<br />

Object[] obs = new Object[0];

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!