CS1335
  • Introduction
  • Assignments
    • A1: Meta, Metta
    • A2: Functions, Emotions
    • A3: Repeat Patterns
    • A4 - Objects: Things and No Thing
    • Final Inspirations
    • A?: Grid Patterns
    • Inspiration
  • Getting Started
    • Processing
    • PDE - Code Editor
    • Learning Processing
  • Java Language
    • Java Syntax
      • Typed-Variables
      • Float - Integer Conversion Errors
      • Modulus
      • Functions
      • Object Reference Data Types
      • Arrays
        • Class Example Code
      • Switch-Case Statement
      • Ternary Operator
      • Class
  • Project 1
    • Random Variation
    • Noise
    • HSB Color Mode
      • HSB Color Wheel
        • Dynamic ColorWheel
        • HSB Color Palette Tool
    • PShape Objects
      • Example Code: PShape
        • Inspiration
    • Modeling Emotions
      • Emotions
        • Kandinsky Color - Emotion
    • PShape with Contour
    • Recursion
      • Recursion Call-Stack
      • Recursion Examples
        • Example Code 1
        • Example Code 2
    • Recursion - PShape
    • Recursive Patterns
    • Planning Structure: Functions:
      • Example Code - Feb 19
      • Final Code Structure
    • Project 1: Programmatic Variations in Color
      • LerpColor
      • Map Function
      • Map with LerpColor
      • noise( )
    • Transforms for Mirroring
    • Project 1-Steps
  • Grid Based Designs
    • Computational Design
    • Artist: Victor Vasarely
    • Grid Pattern Design
    • 1D - Array of PShapes for Grid Layout
      • Truchet Tiling
      • Example Code S2020
      • Example Code March 11
      • Example - March9
      • Example Code
    • PShapes in Grid Regions
    • Grid Region Logic
    • Pattern Preview - Transforms: Translate & Scale
  • Project 2
    • Project 2 - 2D Arrays for Gradient Logic
      • 2D Array Grid with Labels
    • Grid Patterns using 2D Array Indexes: i, j
      • Example Class Code
    • lerpColor( ) and map( ) Functions
    • Demo Lerp Colors
    • 2D Arrays with lerpColor
    • Create PShape 2D Array
    • Function: Populate2DArray( )
    • Function: DisplayShapeMatrix()
    • Transforms for Position, Rotation, Scale of ShapeMatrix Elements
    • Project 2 - Steps
    • Animation for ShapeMatrix
      • Animation w/Noise
  • Object Oriented Programming
    • Introduction to Objects
    • Button States
    • Buttons as Objects
      • Button Class
    • Create Object Instances
    • Button Types
    • Modeling Buttons: States and Events
    • OOP - Inheritance
    • OOP - Polymorphism
    • Child-Class: PImageButton
    • PShape - SVG Objects
    • Menu of Buttons
    • ButtonGroup - Final Version
    • Slider Controller
    • UML Class Diagram
  • Project 3
    • Project 3 - Logic, Steps
    • Project 3 - Class Definitions
      • Button
      • PImageButton
      • ButtonGroup
      • Pattern
        • PShapes - SVG, Vertex Shapes
        • Setting Colors For Patterns
        • Pattern - With Child-PShapes
      • Slider
      • Particles
  • Modeling
    • UML Class Diagram
  • Resources and References
    • Glossary
    • Resources
      • Acoustics
      • Learning Science
        • Emotional Intelligence
      • Creativity
      • Conceptual Art
      • Books
        • Art
      • Games, Rules
      • Complexity
    • Random Inspiration
      • Ulm School
      • Heart-Mind, Mind, Body
      • Statistical Uncertainty
Powered by GitBook
On this page
  • Object Reference-Type Variables: memory address
  • null - Uninitialized Reference Variable
  • Heap: Object Memory-Storage
  • Java: Pass-By-Value

Was this helpful?

  1. Java Language
  2. Java Syntax

Object Reference Data Types

Primitive Data-types: well-defined size:

When we covered Functions, we discussed variable scope, we were discussing how primitive data type variables are defined as being visible, or available for use by the executing program. Primitive data types are what we've been using so far.int, float, boolean, char. The data associated with primitive variables is stored in the program's execution stack, which is where the program's code is located when the program is executing.

Object Reference-Type Variables: memory address

It is important to realize that when we pass object references into a function, then any modifications we make to that object will be persisted to the object after the function has finished executing. In this respect, the rules of local and global variable scope don't affect objects, in the same way they have applied to the primitive data-types we've been using. Variables used to refer to objects are called reference-type variables instead of primitive-type variables. We now need a different way imagine the labeled-box that stores our variable's value, instead of a primitive-value being stored in our box, in this case, the address of the place in memory where our object's data is located, is the value that is stored in our 'variable-box'. This address points to a specific address or memory location where our object's data is stored. So, we are passing the address of an object into a function, this is why changes made in a function are persisted to the object.

To declare an Object Reference-Type Variable, specify the class name to specify the data-type, then provide a custom variable name. The object reference variable has a default value of null which indicates that the variable does not contain the memory address of a valid object. The object reference variable is initialized by calling a special type of class method: constructor methods which have a method name that matches the class name:

Button myButton; //not initialized has default value of null
myButton = new Button( ); //call a constructor 

null - Uninitialized Reference Variable

Button myButton; //declare an object reference variable
if( myButton == null){
    println("myButton doesn't refer to an object in memory, please initialize before trying to use");
}
//use new keyword and call constructor function 
myButton = new Button( );  //this creates an object and assigns memory address to myButton variable
if( myButton != null){
    println("now it's safe to do button things");
}

Heap: Object Memory-Storage

This difference in memory-storage architecture is partially because objects represent much more complicated data-structures. The memory space for holding object data is called 'the Heap'. While we never write code to directly access The Heap is a data-structure with methods to manage the storing object data. When the system executes our program, it may not know exactly how much memory space we need for each object that we create. If we create a Menu object, we may choose to have the menu have 3 buttons, we may choose to have it hold 10 buttons, and we might not even decide how many buttons we want in our Menu object, until the program is executing. In that case, we'd be dynamically determining how large our Menu object is, and that could be different each time our program executes.

Java: Pass-By-Value

There is a subtle distinction between how objects are passed to a function that can vary between different programming languages. Since Processing is based on Java, we need to understand how Java passes variable values into functions. We have specified that when we are working with objects, which are Reference-types, the variable actually contains the memory-address of the object, and that a copy of the address value is passed into the function. If inside the function we modify the address itself, then we break the connection between the object and variable that was holding the address, so changes would occur on whatever object is now pointed to by the function's local address variable.

PreviousFunctionsNextArrays

Last updated 4 years ago

Was this helpful?