A Bunch of Questions About Variables!

Just a few remarks

You can draw an ellipse at position 45,155 without using a variable.

But when you think of it like a ball that is supposed to fly, you get into trouble. You can’t increase 45,155.

Same is true when you want to get the player name for the high score in a game. You can’t put all names like Tim and Anna in the sketch.

Here variables come into play.

Think of a variable as a box with a name on it (x_position) and a content in it (45). Now you can just add something to x_position and the ball flies. You are Independent of the 45 now and can start with a random value instead and just let the ball fly.

fill(255,0,0); 
ellipse(x_position, y_position, 6,6); 
x_position = x_position + 6; 
y_position = y_position + 4;

Similar with the player name.

Of course variables and functions are very common in math, and programming stems from math in a way.

Type

Now in processing/Java the variables additionally have a explicit type which is a good thing. It’s a bit strict but makes the purpose of the variable clearer. It also prevents certain programming errors. A type can be int, float and String etc.

The type tells which values are allowed to get into the box!

Classes and objects

By the way when you think of String or PVector (see reference for both: https://www.processing.org/reference/) these are not only variable types but classes which contain multiple variables and even functions. You can think of them as complex or extended kind of variable types. You can also define your own classes hence your own variable types (e.g. Person with name, address and age).

I simplify things here a bit but you can read the tutorial about objects.

https://www.processing.org/tutorials/objects/

Lists

There are also arrays, ArrayList and the like being lists of variables or objects. These are useful when you want to have a lot of balls or persons: you can for-loop over thousands of them with just a few lines of code.

Chrisir