There are many variable types in processing and it is important to know which variable type suits a particular situation.
int integers (whole numbers) between -2,147,483,648 and 2,147,483,647
float floating point numbers (decimal numbers)
char a single character
byte integers between -127 and 128
boolean true or false
Variable types define what information the variable can store. A variable will always be the same type that was specified when it was declared (created).
In order to create a variable we must declare it to processing by specifying the variable type and name.
int xPosition; declares a new variable xPosition of type int
float yPosition; declares a new variable yPosition of type float
char myFirstInitial; declares a new variable myFirstInitial of type char
byte numOfApples; declares a new variable numOfApples of type byte
boolean lightOn; declares a new variable lightOn of type boolean
Declaring a variable is an important step since the type and name of the variable will be set often for the duration of your program. Variable names cannot contain spaces or most special characters. For this reason there are some conventions for naming variables. The first character or word of a variable name should not be capitalized, but each word thereafter should be capitalized. Check the examples above. This is called camel casing and it is a special convention for naming variables that makes for better readability of code.
In order to use a variable we must initialize it with a value so that our variable name represents some data.
xPosition = 40; sets the variable xPosition to 40
yPosition = 40.5; sets the variable yPosition to 40.5
myFirstInitial = 'M'; sets the variable myFirstInitial to M
numOfApples = 8; sets the variable numOfApples to 8
lightOn = true; sets the variable lightOn to true
You assign variables some data by using the = sign. This is called the assignment operator which assigns the value of anything on the right to the variable on the left. Be careful when assigning values to variables since they should be of the same type.
Sometimes we will want to do these operations at once. Here is an example
float yPosition = 40.5; declare and initialize variable yPosition to 40.5
Now we can see what value yPosition is by calling the print statement with yPosition as an argument.
Which will print out 40.5
Errors can arise when the value being assigned to the variable is of a different type. Here are some examples:
int xPosition = 40.5; cannot assign float to type int
boolean lightOn = 8; cannot assign int to type boolean
char myFirstInitial = "ML"; cannot assign string to type char
float yPosition = 40; works, but why?
In the last example, the assignment of the integer value 40 to a float worked. This is only because a floating point number can represent an integer value as simply 40.0.
Another type of error that is common is forgetting to initialize your variable. Usually processing will set this variable to 0 if it is an int, byte, float, an empty string if it is a char, and false if it is a boolean. These may not be the desired values for the variables in your program so you MUST always initialize your variables.