Primitive Data Types


The crucial variable in the process of turning knowledge into value is creativity. [John Kao]


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).

Variable types define what type of information a variable holds.


In order to create a variable we must declare it to processing by specifying the variable type and name.

int xPosition; declare variable xPosition of type int
float yPosition; declare variable yPosition of type float
char aLetter; declare variable aLetter of type char
byte numApples; declare variable numApples of type byte
boolean lightOn; declare 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.

Variable types and names are often set for the duration of a program, so declare variables wisely.
Variable names should be Camel Cased, first word lower case, each word after capitalized.


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.

Declaring and Initializing

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

Variables must be assigned only values that their type will accept.


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.

Do not assign values of a different type to a variable. You must use the type specified when the variable was declared.
Always initialize your variables, do not leave things to chance.