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.

It’s because strict typing helps create more precise <strong>and</strong> ultimately faster-running code. If<br />

you try to assign a float value—for example, 3.2—to int ballCount, <strong>Processing</strong> will spit<br />

back a compiler error that says<br />

The type of the right sub-expression, "float", is not assignable to<br />

the variable, of type "int".<br />

This may seem obnoxious <strong>and</strong> a royal pain, especially if you are used to a loosely typed<br />

language, but it really is a useful (or at least justifiable) language feature. Save for the<br />

strange worlds of quantum physics, we would expect distinct whole balls to be held in<br />

ballCount—not parts of balls. If the compiler let you assign 3.2 (maybe entered due to a<br />

typo) to ballCount, your program would probably crash later on. To debug your program,<br />

you would then have to track down a tiny but fatal type conversion error (definitely not a<br />

happy coding accident). Additionally, in languages that do permit on-the-fly data type<br />

assignment, it takes more work for the computer to figure out what type of data is held in<br />

a variable.<br />

Because you set the data type of variables when they are declared in <strong>Processing</strong> <strong>and</strong> Java,<br />

<strong>and</strong> that information gets put in the compiled bytecode, the virtual machine (the language<br />

interpreter) needs to do less work. Languages like <strong>Processing</strong> <strong>and</strong> Java that work this way<br />

are called statically typed languages. Languages like Python, on the other h<strong>and</strong>, are<br />

dynamically typed, <strong>and</strong>, due in part to dynamic typing, run slower than Java <strong>and</strong><br />

<strong>Processing</strong>. Now, at the risk of really getting you rankled, what do you think would happen<br />

if you tried to assign 3 (an integer) to ballSpeed (declared as type float)? Surprisingly,<br />

you wouldn’t get a compiler error. Huh! That seems to go against what I just wrote about<br />

how static typing works. The reason the compiler allows it is because the compiler converts<br />

the 3 to 3.0 automatically. Let’s test this out. If I try running the following code:<br />

float ballSpeed;<br />

ballSpeed = 3;<br />

println(ballSpeed);<br />

I get the following output:<br />

3.0<br />

The reason the compiler lets me get away with this is because there is no danger, nor any<br />

real change, to the value. 3, 3.0, <strong>and</strong> even 3.0000000000000 are all equal. Not to get too<br />

geeky, but this type of implicit conversion is called a “widening conversion,” where an int<br />

is automatically converted into a float. The rule is that any primitive type will be converted<br />

implicitly (without any help from you) into any other type only if that type supports<br />

a larger value; a float supports a larger value than an int. In regard to <strong>Processing</strong>, that<br />

would include byte to int <strong>and</strong> int to float. Bytes can become integers <strong>and</strong> integers can<br />

become floats. Converting the other way, from a float to an int to a byte (a “narrowing<br />

conversion”) can actually also happen, but you have to do it explicitly (which involves a<br />

little work on your part), with something called type casting, which I’ll cover later in the<br />

book. For now, let’s put this theory stuff to bed <strong>and</strong> play with variables a little.<br />

Here’s a little program to create a stepped radial gradient (see Figure 3-2). Later on when<br />

I cover loops, I’ll simplify this program, <strong>and</strong> also show you how to generate a more elegant<br />

continuous gradient. You’ll create the program in three stages. The first stage will fill the<br />

CODE GRAMMAR 101<br />

67<br />

3

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

Saved successfully!

Ooh no, something went wrong!