Model of Execution

Interpretation v. Compliation

  • Interpretation
    • Code is read and executed one statement at a time
  • Compliation
    • Entire progeam is translated into another language
    • The translated code is interpreted

Interpretation

  • Python and JavaScript are interpreted languages
  • Run-time error are common
    • Program runs, but crashes when a line with an error is interpreted

This program runs without error

1
2
3
4
5
6
7
8
9
10
11
12
13
class RuntimeErrorExample:

def _init_(self, initial_state):
self.state = initial_state

def add_to_state(self, to_add):
println("adding to state")
self.state += to_add

if _name_ == '_main_':
example_object = RuntimeErrorExample(5)
example_object.add_to_state(10)
print(example_object,state)

This program crashes with runtime error

1
2
3
4
5
6
7
8
9
10
11
12
13
class RuntimeErrorExample:

def _init_(self, initial_state):
self.state = initial_state

def add_to_state(self, to_add):
println("adding to state")
self.state += to_add

if _name_ == '_main_':
example_object = RuntimeErrorExample(5)
example_object.add_to_state("ten")
print(example_object,state)

Compilation

  • Scala, Java, C, and C++ are compiled language
  • Compiler errors are common
    • Compilers will check all syntax and types and alert us of any errors (Compiler error)
    • Program fails to be converted into the target language
    • Program never runs
    • The compiler can help us find errors before they become run-time errors

Compilers and runs without error

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class CompileError(var state: Int) {
def addToState(toAdd: Int): Unit = {
println("adding to state")
this.state += toAdd
}
}

object Main {
def main(args: Array[String]): Unit = {
val exampleObject = new CompilerError(5)
exampleObject.addToState(10)
println(exampleObject.state)
}
}

Does not compile. Will not run any code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class CompileError(var state: Int) {
def addToState(toAdd: Int): Unit = {
println("adding to state")
this.state += toAdd
}
}

object Main {
def main(args: Array[String]): Unit = {
val exampleObject = new CompilerError(5)
exampleObject.addToState("ten")
println(exampleObject.state)
}
}

Compilation - Scala

  • Scala compiles to Java Byte Code

  • Executed by the Java Virtual Machine (JVM)

    • Installed on Billions of devices!
  • Compiled Java and Scala code can be uesd in the same program

    • Since they both compile to Java Byte Code
  • Scala uses many Java classes

    • We saw that Math in Scala is Java’s Math calss
    • We’ll somtimes use Java libraries in this course

References

  • Every class you create will be passed by reference
    • Also data structure (List, Map, Array) and other built-in calsses
  • Pass-by-reference means that a copy is not made when a variable is assigned a value
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
object ItemReferences {
def increasePrice( item: Item): Unit = {
item.price += 0.25
}

def main(args:Array[String]): Unit = {

val cereal: Item = new Item("cereal", 3.0)

// pass-by-reference
increasePrice(cereal)

// assignment-by-reference
val cereal2: Item = cereal

increasePrice(cereal2)

// 3.5
println(cereal.price)
}
}
  • increasePrice returns Unit, yet it is able to modify an item
  • cereal and cereal2 “refer” to the same obejct
    • Changes made to one will change both variables

References: Warning

1
2
3
4
5
def updateObject(dynamicObject: DynamicObject, deltaTime: Double, magnitudeOfGravity: Double): Unit = {
dynamicObject.previousLocation = dynamicObject.location

// ... rest of the method
}
  • previousLocation and location are the same object!!!
    • Changing location will change previousLocation
  • Create a new PhysicsVector for previousLocation or copy x,y,z one at a time

Memory

  • Random Access Memory (RAM)
    • Access any value by index
    • Effectively a giant array
  • All values in your program are stored here

Let’s Talk About Memory

  • Significantly faster than reading / writing to disk

    • Even with a SSD
  • Significantly more expensive than disk space

  • Operating System (OS) controls memory

  • On Program start, OS allocates a section of memory for our program

    • Gives access to a range of memory addresses / indices
  • Some space is reserved for program data

  • Details not important to CSE116

  • The rest will be used for our data

  • Data stored in the memory stack

Memory Stack

  • Stores the variables and values for our programs

  • LIFO - Last In First Out

    • New values are added to the end of the stack
    • Only values at the end of the stack can be removed
  • Method calls create new stack frames

    • Active stack frame is the currently executiong method
    • Only stack values in the current stack frame can be accessed
    • A stack frame is isolated from the rest of the stack
  • Program execution begins in the mian method stack frame

  • Code blocks control variable scope

    • Code executing within a code block (ex. if, for, while)
      begins a new section on the stack
  • Similar to stack frames, but values outside of the code block can be accessed

  • Variables / Values in the same code block cannot have the same name

    • If variables in different blocks have the same name, the program searches the inner-most code block first for that variable
  • When the end of a code block is reached, all variables / values created within that block are destoryed

Memory Stack Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
function computeFactorial(n){
result = 1
for (i = 1; i < n; i++) {
result *= i
}
return result
}

function main(commandLineArgs){
i = 5
n = computeFactorial(i)
print(n)
}

Note: This example is language independent and will focus on the concept of memory. Each language will have differences in how memory is managed

No contents showing, check PDF

Stack Memory

  • Only “primitive” types are stored in stack memory
    • Double / Float
    • Int / Long / Short
    • Char
    • Byte
    • Boolean
  • Values corresponding to Java primitives
    • Compiler converts these objects to primitives in Java Byte Code

Memory Heap

No contents showing, check PDF

Lecture Question

Question: In a package named “physics” create a Scala class named “PhysicsVector” with the following:

  • A constructor that takes 3 variables of type Double named “x”, “y”, and “z”
  • A method named “multiplyByConstant” that takes a Double and returns Unit. This method multiplies x, y, and z by the input
    • Be sure to update the state variables of the object when this method is called
    • Example: If a vector with x, y, and z of (2.0, 0.0, -1.5) has multiplyByConstant(2.0) called on it, it’s state will become (4.0, 0.0, -3.0)
  • A method named “addVector” that takes a PhysicsVector and returns Unit. This method adds the values of x, y, and z of the input vector to the state variables of the calling vector
    • Example: If a vector with x, y, and z of (2.0, 0.0, -1.5) has addVector(otherVector) called on it where otherVector is (-3.5, 0.4, -1.0), it’s state will become (-1.5, 0.4, -2.5)

Testing: In a package named “tests” create a Scala class named “TestVector” as a
test suite that tests all the functionality listed above