CSE116的学习笔记-Lec#6:Model of Execution
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 | class RuntimeErrorExample: |
This program crashes with runtime error
1 | class RuntimeErrorExample: |
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 | class CompileError(var state: Int) { |
Does not compile. Will not run any code
1 | class CompileError(var state: Int) { |
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 | object ItemReferences { |
- 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 | def updateObject(dynamicObject: DynamicObject, deltaTime: Double, magnitudeOfGravity: Double): Unit = { |
- 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
- Code executing within a code block (ex. if, for, while)
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 | function computeFactorial(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