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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Variables<br />

Variables are essential to programming, <strong>and</strong> it is hard to imagine coding anything without<br />

them. In <strong>Processing</strong> <strong>and</strong> Java, there are two types of variables: primitive variables <strong>and</strong><br />

object reference variables. The major differences between these two categories of variables<br />

are the kind of data they can be associated with <strong>and</strong> how this data is stored <strong>and</strong> referenced<br />

in memory. I’m only going to cover primitive variables here; I’ll discuss object<br />

reference variables when I cover OOP in Chapter 8.<br />

A primitive variable is a simply a name assigned a specific value in memory that you can<br />

recall <strong>and</strong> change. Here are a couple of examples of primitive variables:<br />

int age = 6;<br />

String name = "Ian";<br />

boolean isABoy = true;<br />

The first part of each of the statements (int, String, <strong>and</strong> boolean) declares what type of<br />

primitive data the variable can hold: int holds integers, String holds words, <strong>and</strong> boolean<br />

holds true or false. The next part (age, name, <strong>and</strong> isABoy) is the identifier of the variable,<br />

<strong>and</strong> the last part is the actual value (6, "Ian", <strong>and</strong> true) assigned to the variable.<br />

Variable names in <strong>Processing</strong> <strong>and</strong> Java are case sensitive, meaning that xSpeed is a different<br />

variable than xspeed. A variable’s name is created by the coder <strong>and</strong> needs to be a legal<br />

identifier, which was described previously. Regardless of whether you are naming a primitive<br />

variable, object reference variable, function, method, class, or object, the same legal<br />

identifier–naming rules apply. There are also some reserved keywords that you should<br />

avoid using when naming things. The reserved keywords can be found here: http://<br />

java.sun.com/docs/books/tutorial/java/nuts<strong>and</strong>bolts/_keywords.html. In addition, I<br />

would also strongly recommend not naming your custom variables, functions, objects, <strong>and</strong><br />

so forth with any of the built-in function names in <strong>Processing</strong>; it will lead to, at best, unexpected<br />

results.<br />

When I initially create a variable by writing int ballCount, I am not yet specifying a specific<br />

value to be assigned to the variable, but only letting the computer know what type of<br />

data is allowed to be assigned to the variable (in this case, an integer). Every variable must<br />

be associated with a data type. Some common primitive data types in <strong>Processing</strong> are int,<br />

float, char, <strong>and</strong> boolean. Data types tell the variables what they can or can’t do, <strong>and</strong> also<br />

how much memory should be allocated for them. For example, a boolean type variable<br />

can only hold the values true or false, requiring only 1 bit of memory to store either a<br />

single 0 (false) or a 1 (true). It would be a waste of memory to store it any other way. A<br />

byte can hold a number between –128 to 127 (that’s 256 different possible values), which<br />

requires 8 bits, or 1 byte, of memory.<br />

CODE GRAMMAR 101<br />

65<br />

3

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

Saved successfully!

Ooh no, something went wrong!