edit upload recent print


* WikiDocs

Main » Variables


Variables are containers for data. Essentially a variable is something that can hold a value and that value can be changed during a program.

Data Types

Variables in Processing (and other typed languages) must be defined or declared with their type.

Some of the common types that Processing/Java support are as follows:

  1. int - An integer. Whole numbers only. int someInt = 5;
  2. boolean - True or False. Often the result of an expression. boolean someBool = true;
  3. float - A number with a decimal point. 1.5, 3.987 and so on. float someFloat = 99.76;
  4. char - A single character. char myChar = 'A';
  5. String - A sequence of characters. String myString = "Hello World";


Variables must be declared with their type as shown (in the examples above as well as) here:

int myInteger; float yourFloat; boolean myBool; String myString;


Once they are declared, you are free to assign values to them and subsequently use them in operations:

myInteger = 5; myInteger = 5 + 5; myInteger = myInteger - 5; yourFloat = 5.5; myString = "goodbye";

Of course, as shown in the examples above, you can take a shortcut and do both declaration and assignement in one step:

int someInt = 89; String myString = "this works too";

Variable Scope

You may have noticed how we often declare are variables at the top of a processing program, outside of the setup() and draw() functions. This is due to something called 'variable scope'. The concept is that variables are only accessible within the block of code where they are declared.

For instance, if we declared a variable within in the setup() function, we would not be able to access that variable in the draw() function.

Blocks of code are defined by "{" and "}". Any code that is within the brackets is considered in the same block. Therefore, setup() and draw() have their own blocks of code.

A code block:

    // Some code
    // Some more code

If you declare a variable outside of a block of code, in the main processing code section it is a global variable. If you declare it inside of a block of code, it is a local variable. Local to that block.

A global and a local variable:

  int myInt = 90;  // Global [available anywhere]

  void setup()
    int myOtherInt = 100; // Local [available only in setup()]

Built-in Variables

Processing has many convenient 'built-in' variables that make it easier for you to write your programs. Examples we've seen so far include "mouseX" and "mouseY"; which store the location of the mouse in relation to the program window.

Example #1 Follow the cursor around:

        void setup()

        void draw()

Two others we've seen are: "width" and "height", which store the width and height of a Processing window. These are only available after size() is called (generally the first line of setup()).

        void setup()
          println("The width is: " + width);
          println("The height is: " + height);

NEXT: Arrays

Instructor: Daniel C. Howe



Page last modified on April 11, 2007, at 12:49 AM EST