Processing Uses Java. Here Are Some Important Java/Python Differences: 1. Coding Syntax (What You Type)

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 4

Creative Coding cheat sheet

Processing uses Java. Here are some important Java/Python differences:

1. Coding syntax (what you type)


● Java expects a semicolon at the end of each line of code.
● Functions and for loops are contained within curly brackets {}.
● Conditional statement tests (if) and function calls are surrounded by round brackets
().
● Java doesn’t care about new lines and tabs.

Example:

void draw()
{
// print the integers from 1 to 9
for (int i = 1; i < 10; i++) // set up the for loop with an i integer
{
println(i); // output i to the console
}
}

2. Variables & data types


Python lets you declare variables and change their data type (e.g. start off with hello =
“hello” string, and later use it as a number. Java is statically typed - you have to explicitly
declare all variables with a data type before you try and assign or use them.

// initialise the placeholder for some variables


String hello;
int frogs;

// set their values


hello = “hello, world!”;
frogs = 99;

We’ll meet the following data types today:

int An integer i.e. a whole number e.g. int frogs = 99;

boolean Either true or false e.g. boolean play = true;

String text, declared in quote marks e.g. String hello = “hello”;

float A number with a decimal point e.g. float dec = 10.5;


To program in Processing, you have to decide up front what sort of data type your
variable is, and you can’t change it afterwards. For example, if you try and set a
String variable as the number 5:

String hello = 5;

Processing will highlight that line and tell you that you can’t convert from an int to a
String.

Scope - or where to create your variables


Something to be careful with is where in your code you create your variables. If you
create them at the top of your script (outside setup() and draw()), then you can
access them throughout your sketch. However, if you create a variable inside
setup(), you can only access the information it contains within that method - the
variable isn’t stored once you reach the end of the method.

int frogs; // make frogs available throughout the whole sketch

void setup()
{
frogs = 99;
// a local variable that only exists inside this function
String hello = “hello, world!”;
}

void draw()
{
frogs += 99; // we can access the frogs variable here
// this will give an error (cannot find anything named “hello”)
// because the hello variable
// only exists inside the setup function!
hello = “burp”;
}

The exception to this are special Processing global variables, which basically means
variables you can access anywhere. Really useful Processing ones are width and
height, which after the size() method has been called hold the width and height
info for the sketch. Take a look at the pink cheatsheet to see some others.
Comments
Comments are ignored by Processing, but they are really handy for making notes to
yourself about what you are trying to do, or for what a variable is for. You’ll see a
lot of comments in the code today!

There are 2 sorts of comments you can make:

// anything on this line is ignored


/* everything between the opening slash star
is ignore by Processing until you get to
a closing star slash */

Common errors and what they mean

Cannot find anything called “x” Processing can’t find the x variable. Is it in scope
(see above)?

Cannot convert from x to y You’re trying to assign a value to a variable


that’s different to its declared type (e.g. trying to
store a number in a String variable).

NullPointerException You’re trying to use a variable that doesn’t have


a value yet (it’s null).

Syntax error Probably missing a semicolon. Check the line


above where the error is highlighted - can you
see anything missing on it?

unexpected token Probably missing a closing bracket }. Check the


line above the highlighted error.

The function somefunction() Processing can’t find the function you refer to. Is
does not exist there a typo in your function call?

The method method(x,y) is not You are calling that method incorrectly! Check in
applicable for the the reference to see what data you need to
arguments(a,b). specify in the brackets.

IndexOutOfBoundsException You’ve tried to access an array slot that doesn’t


exist – e.g. the 10th slot in an array with 4 slots.
Methods vs functions
Officially, a function is a piece of code that is called by name (e.g. draw()). A
method is also a piece of code that is called by name, but it is associated with an
object (e.g. cam.start()). That said, they are very similar and often used
interchangeably. Forgive me if I use the wrong name today!

Extra learning resources


● https://www.processing.org
● Absolutely everything on this site is helpful! I would highly recommend
the “Hello Processing” tutorial in particular.
● http://funprogramming.org
● https://amnonp5.wordpress.com/2012/01/28/25-life-saving-tips-for-
processing/
● https://www.futurelearn.com/courses/creative-coding - online course using
Processing

You might also like