Typed-Variables
Variables can be considered as named containers to hold values that can be modified. Since Processing is based on the Java language, it uses strongly-typed variables, this means that a variable must be declared as a specific data-type before it can be used in a program; in addition, once declared, the variable cannot be modified to refer to a different type of data later in the program.
When representing data in our programs, we can consider hard-coded values as literal values, and we can consider 2 main categories of variables: primitive-type (value-type) , or composite (reference-type) variables,.
Literal Values
When we initialize variables, we often write a number or character symbol to represent a fixed value such as 10
for the numeric value 10. When we use literal values, these are associated with a specific type of data, for example, 10 is an integer-type literal, while 10.0 is a floating-point type of literal value.
Primitive-Data Types
Primitive-type variables involve a single piece of information like integers: int
, decimal numbers: float, double
, booleans: boolean
, and characters: char
. Primitive-types use a fixed, system-defined size of memory-space. Primitive data-types (also called value-types) are pre-defined as part of the Java language, the primitive-type names are Java reserved keywords. Java Reference
Object Data Types
Composite-type values refer to more complex data-types like arrays and objects; the memory-size required storing the data elements for complex variables, is not as easy for the system to determine when the program is being compiled. For this reason, a variable of this type actually refers to the memory-address of the starting location in memory where the data is stored. If the reference-variable does not contain a valid memory address, then it has a value of 'null'. Typically the remainder of the data is stored in adjacent memory locations. Composite-type variables are commonly called reference
data types, these include Array, ArrayList, String, and custom Class-Objects. Reference
variables refer
to a location in memory where the data is stored.
Primitive vs. Reference Data-types: Function Parameters
A major difference in behavior exists between primitive and reference data-types when they are defined as input parameters for a function. Primitive-type variables are passed-by-value
into a function, this means that only a copy of the argument value is passed into a function, so any changes made to a local parameter within a function do not modify the value of the original variable. This is related to variable scope, when a primitive-type variable is passed as an argument to a function, changes made to the function parameter within the function body do not impact the original primitive variable. However, when composite, reference-type variables are passed into a function as arguments, then a copy of the reference-type variable is passed into the function, this means the memory address of the object is passed into the function. Therefore, changes made within the function body to the referenced element are actually being made to the original data element. However, it's important to notice that it is a copy of the reference variable that is passed into the function, so, changes to the actual local function reference variable, such as setting it to null, do not affect the original data element.
Object Variables
When we create custom Java Classes, we can define variables that will be associated with object-instances when a custom object is created in our code. There are 2 primary types of variables associated with Objects: Instance variables and static-Class variables. Instance variables are associated with an instance of an object, so the values of instance variables is likely different for each instance of an object that is created. Static variables can be created as a special type of class variable, these are associated with the Class itself.
Declaration and Initialization
In the example code below, the first line of code prints the sum of 2 integer literal values. In the code that follows, int
and float
variables are declared and assigned values.
Typed Variables
When using P5js and the Khan-Academy Javascript Tutorials, variables were all of the type var
. There was no distinction between different types of variables. However, with The Java Version of Processing, all variables must be declared as a specific data-type such as int
, float
, boolean
, char
, etc. Typed variables allows the computer to allocate enough memory to hold the value.
Type Conversion
When our programs are executing, the system will try to convert variables to the same data-type, and there are important reasons why we need to be aware of how the system does automatic type-conversion. In Java, the compiler checks our code to insure our code doesn't do any auto-type conversions that might cause a mathematical error in our program. The compiler follows strict rules to determine the validity of our mixed-type expressions.
Integers
Whole numbers like -1, 0, 1, 2.
Integer Division
When using the math division operator with integers, the resulting value is also an integer, so any fractional division remainder is truncated
Floats
Decimal numbers like 1.0, 5.5, -1.0. Also, whole numbers like 1, 0, 1 can be stored as floating point numbers.
When initializing floating point numbers which are created using math operators, it's important to realize that integer division can cause unexpected results. Multiplying each division expression by the float
value 1.0 can help insure no truncation occurs.
In the code below, since both 2 and 5 are written as integer literals, then expression 5/2 is evaluated using integer division. Make sure that at least 1 value is a decimal value to insure correct division of numbers assigned to float
variables:
Integer and Float Type-Conversion
Care must be taken when using float
and int
variables in expressions or mathematical operations together, particularly when doing division. In general, an error will be generated if an operation will result in truncation. Processing can automatically convert an int
to a float
value, however an error will occur when trying to convert a float
value to an int
value.
Processing provides type conversion functions to allow conversion between int
and float
variable types. There are 2 different but equivalent syntax conventions for type conversion displayed in the example code below:
Modulus Operator
The modulus operator %
calculates the remainder of integer division. Modulus is often used to determine if a number is odd or even where n % 2 equals 0 if n is even. Modulus restricts values to within a limited range, example: someVal % 12; In the case where 12 is the value to the right of the modulus operator, values can range from 0 to 11, always 1 less than the modulus variable.
Boolean
Boolean variables can have the value true
or false
; Boolean variables are useful for storing the state some program element to control some branch option within the program, often within a conditional branch, the boolean variable value is changed to indicate the state of the program has changed.:
Character
Single letters or other unicode symbol like 'a', 'b', 'A', '%' . The char
variable type must use single quotes around a single character. When multiple characters are used in a single variable, then the :code: String variable type should be used.
Operators
Random Numbers
The random( )
function in Processing can be used to generate psudo-random variables. The random(float min, float max)
function takes 2 input parameters and returns a floating point number ranging from the first argument to the second argument. If only 1 argument is used, then 0 is the default minimum value.:
Questions
What are the values of the following?
int num1 = 2 % 10;
int num2 = 10 % 2;
int num3 = int(4.999);
Last updated