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 />

680<br />

Composite<br />

The Composite section includes data types used to store object references (the memory<br />

addresses I mentioned in the primitive data type discussion). The three composite types<br />

Array, Object, <strong>and</strong> String can each hold multiple pieces of data.<br />

Arrays are collections of any other data type, referenced by a single variable name. Arrays<br />

can only hold one data type at a time, but lots of it. I cover arrays in detail in Chapter 3.<br />

Objects are the main units in object-oriented programming. An object is an instance of a<br />

class <strong>and</strong> includes copies of the properties <strong>and</strong> methods defined within the class. Objects<br />

are covered in much greater depth in Chapter 8 in the discussion of object-oriented programming<br />

(OOP).<br />

Strings are kind of like arrays that only hold characters (e.g., letters). There are two ways<br />

to create String objects in <strong>Processing</strong> <strong>and</strong> Java (which is confusing):<br />

<strong>and</strong><br />

String s1 = "hello";<br />

String s2 = new String("hello");<br />

The first way looks like a normal primitive declaration, like you would use to declare an<br />

int (int count = 3;), a float (float speed = .5;), <strong>and</strong> so on. The second way looks like<br />

st<strong>and</strong>ard object creation, called instantiation, which uses the st<strong>and</strong>ard object-oriented syntax<br />

ClassType variableName = new ClassType();. Based on what I’ve told you about<br />

data types up to this point in the book, this may not make sense. How can a string represent<br />

both a primitive data type <strong>and</strong> also a reference (composite) data type? Well, in truth,<br />

there is no primitive String data type in Java or <strong>Processing</strong>; you’re always creating a<br />

String object. The shortcut approach String s1 = "hello"; internally creates a String<br />

object from the literal (the word in quotes). This is all fine <strong>and</strong> good, as well as probably a<br />

little overly technical. However, if you eventually find yourself needing to compare two<br />

strings <strong>and</strong> test them for equality, these two different string-creation approaches have<br />

some confusing (<strong>and</strong> probably annoying) differences.<br />

As an example, run the following:<br />

String s1 = "hello";<br />

String s2 = new String("hello");<br />

println(s1 == s2);<br />

String s3 = "goodbye";<br />

String s4 = "goodbye";<br />

println(s3 == s4);<br />

The output tells you that s1 <strong>and</strong> s2 are not equal, but s3 <strong>and</strong> s4 are. Even though the two<br />

values being compared in each comparison are the same (hello <strong>and</strong> hello <strong>and</strong> goodbye<br />

<strong>and</strong> goodbye), the output is different because of the different ways the strings were created.<br />

I won’t bore you further with why this occurs, but you can read more about it here:<br />

http://java.about.com/library/weekly/aa_strings1.htm. Be sure to check the second

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

Saved successfully!

Ooh no, something went wrong!