Methods & Activation Record. Recap: what’s a method?

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

Topic 10 Java Memory Management. 1-2 Memory Allocation in Java When a program is being executed, separate areas of memory are allocated for each class.
Recursion CS 367 – Introduction to Data Structures.
CSE 332: C++ exceptions Overview of C++ Exceptions Normal program control flow is halted –At the point where an exception is thrown The program call stack.
Unit 181 Recursion Definition Recursive Methods Example 1 How does Recursion work? Example 2 Problems with Recursion Infinite Recursion Exercises.
Run-Time Storage Organization
1 Memory Model of A Program, Methods Overview l Closer Look at Methods l Memory Model of JVM »Method Area »Heap »Stack l Preview: Parameter Passing.
1 Memory Model of A Program, Methods Overview l Memory storage areas for an executing program l Introduction to methods and methods definitions l General.
1 Memory Model of A Program, Methods Overview l Memory Model of JVM »Method Area »Heap »Stack.
Macro & Function. Function consumes more time When a function is called, the copy of the arguments are passed to the parameters in the function. After.
1 BUILDING JAVA PROGRAMS CHAPTER 3 THE SCANNER CLASS AND USER INPUT.
Sadegh Aliakbary Sharif University of Technology Spring 2011.
SE-1010 Dr. Mark L. Hornick 1 Defining Your Own Classes Part 3.
Computer Architecture and Operating Systems CS 3230 :Assembly Section Lecture 7 Department of Computer Science and Software Engineering University of Wisconsin-Platteville.
Runtime Environments Compiler Construction Chapter 7.
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
Problem of the Day  Why are manhole covers round?
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Methods (a.k.a. Functions)
Lecture 9 Using Objects. Remember: 3 Different Kinds of Classes 1.Application Class – what we've been doing – Has public static void main ( String []
Procedural programming in Java Methods, parameters and return values.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 6.
1 Announcements Note from admins: Edit.cshrc.solaris instead of.tcshrc Note from admins: Do not use delta.ece.
Java - Classes JPatterson. What is a class? public class _Alpha { public static void main(String [] args) { } You have been using classes all year – you.
Methods: A Deeper Look. Template for Class Definition public class { } A.Import Statement B.Class Comments C.Class Name D.Data members E.Methods (inc.
Introduction to Computing Concepts Note Set 15. JOptionPane.showMessageDialog Message Dialog Allows you to give a brief message to the user Can be used.
1 Chapter 6 Methods. 2 Motivation Find the sum of integers from 1 to 10, from 20 to 30, and from 35 to 45, respectively.
Bank Account Example public class BankAccount { private double balance; public static int totalAccounts = 0; public BankAccount() { balance = 0; totalAccounts++;
Week 8 - Friday.  What did we talk about last time?  Static methods.
Files Review For output to a file: –FileOutputStream variable initialized to filename (String) and append/not append (boolean) –PrintWriter variable initialized.
Chapter 5 : Methods Part 2. Returning a Value from a Method  Data can be passed into a method by way of the parameter variables. Data may also be returned.
Method Examples CS 139 Algorithm Development 10/06/2008.
AP Computer Science A – Healdsburg High School 1 Unit 9 - Parameter Passing in Java.
import java.util.Scanner; class myCode { public static void main(String[] args) { Scanner input= new Scanner(System.in); int num1; System.out.println(“Enter.
1 This week Basics of functions Stack frames Stack vs. Heap (brief intro) Calling conventions Storage classes vs. scope Library functions Overloading.
Tarik Booker CS 242. What we will cover…  Functions  Function Syntax  Local Variables  Global Variables  The Scope of Variables  Making Functions.
“Success consists of going from failure to failure without loss of enthusiasm.” Winston Churchill.
Staples are our staple Building upon our solution.
Variable scope. Variable Scope Variables do not live forever. Failing to take that into account leads to problems. Let's look at an example. Let's write.
Java Memory Management
Chapter 2 Clarifications
Java Memory Management
using System; namespace Demo01 { class Program
Week 8 - Friday CS 121.
Lecture 10: More on Methods and Scope
Programming Fundamentals Lecture #7 Functions
Computer Programming Methodology Input and While Loop
TK1114 Computer Programming
Something about Java Introduction to Problem Solving and Programming 1.
Chapter 6 Methods: A Deeper Look
Functions Inputs Output
Starting Out with Java: From Control Structures through Objects
Stack Memory 2 (also called Call Stack)
Week 6 CS 302 Jim Williams, PhD.
Defining methods and more arrays
Code Animation Examples
CS2011 Introduction to Programming I Arrays (II)
Chapter 6 Methods.
class PrintOnetoTen { public static void main(String args[]) {
Methods and Data Passing
Scope of variables class scopeofvars {
When a function is called...
Methods and Data Passing
Scope scope: The part of a program where a variable exists. From its declaration to the end of the { } braces A variable declared in a for loop exists.
Local variables and how to recognize them
Consider the following code:
Lecture 9 Using Objects.
Basic Guarantees Right off the bat… what I'm going to describe is what almost certainly happens on contemporary machines, but there is absolutely nothing.
Corresponds with Chapter 5
Implementing Functions: Overview
Chapter 6: Methods CS1: Java Programming Colorado State University
Presentation transcript:

Methods & Activation Record

Recap: what’s a method?

Method Headers public static void main(String[] args) {}

Method Headers public class Car { … public double getSpeed(); } public class Math { … public static double abs(double a); } Car c = new Car(); double s = c.getSpeed(); double a = Math.abs(-4);

Scope What happens in the curly braces, Stays in the curly braces. How does this work with methods?

Example Code: Does this work? public static void main(String[] args) { Scanner sc = new Scanner(System.in); double a = sc.nextDouble(); double sum = addFive(); } public static void addFive() { double sum = a + 5; }

main() Scanner sc double a double sum = ? public static void main(String[] args) { Scanner sc = new Scanner(System.in); double a = sc.nextDouble(); double sum = addFive(); // executing this line } String[] args

addFive() main() Scanner sc double a double sum = ? public static void addFive() { double sum = a + 5; } String[] args ?!?!?!?!?!?!?!?!

Let’s fix that… public static void main(String[] args) { Scanner sc = new Scanner(System.in); double a = sc.nextDouble(); double sum = addFive(a); } public static void addFive(double a) { double sum = a + 5; }

main() Scanner sc double a double sum = ? public static void main(String[] args) { Scanner sc = new Scanner(System.in); double a = sc.nextDouble(); double sum = addFive(a); // executing this line } String[] args

addFive() main() Scanner sc double a double sum = ? public static void addFive(double a) { double sum = a + 5; } String[] args double a Note that main() still has the original variable a – addFive() just has a COPY. We call this “pass by value.” double sum = a + 5

main() Scanner sc double a double sum = ? public static void main(String[] args) { Scanner sc = new Scanner(System.in); double a = sc.nextDouble(); double sum = addFive(a); // we’re back to this line } String[] args

Example Code public static void main(String[] args) { Scanner sc = new Scanner(System.in); double a = sc.nextDouble(); double sum = addFive(a); } public static double addFive(double a) { return a + 5; }

main() Scanner sc double a double sum = public static void main(String[] args) { Scanner sc = new Scanner(System.in); double a = sc.nextDouble(); double sum = addFive(a); // executing this line } String[] args

addFive() main() Scanner sc double a double sum = public static int addFive(double a) { return a + 5; } String[] args double a a + 5

main() Scanner sc double a double sum = public static void main(String[] args) { Scanner sc = new Scanner(System.in); double a = sc.nextDouble(); double sum = addFive(a); // we’re back to this line } String[] args a + 5

Let’s draw the stack frame a bit cleaner. Local Variables Return address Parameters

Now get out a sheet of paper… public static void main(String[] args) { int product = multiply(5, 10); } public static int multiply(int a, int b) { int product = 0; for (int i = 0; i < b; ++i)// at i = 0 product += a; return product; }

int i = 0 int product = 0 [address of main()] int b = 10 int a = 5 [address in system] String[] args MAIN MULTIPLY

What about now? public static void main(String[] args) { int product = multiply(5, 10); } public static int multiply(int a, int b) { int product = 0; for (int i = 0; i < b; ++i) product += a; return product; }

int product = 50 [address of main()] int b = 10 int a = 5 [address in system] String[] args MAIN MULTIPLY

Another example public static void main(String[] args) { double r = 54.56; int num = round(r); } public static int round(double d) { double dec = d – (int) d; int ret; if (dec < 0.5) ret = Math.floor(d); else ret = Math.ceil(d); return ret; }

int ret; double dec = 0.56; [address of main()] double d = double r = [address in system] String[] args MAIN() ROUND()

Another example public static void main(String[] args) { int num = A(10); } public static int A(int i) { return B(i + 2); } public static int B(int i) { int r = i; int p = r + i; return i + p + r; }

int p = 24 int r = 12 [address of A()] int i = 12 [address of main()] int i = 10 [address in system] String[] args MAIN() A() B()

To Sum Up Scope – What happens in the curly braces, stays in the curly braces This concept translates into chunks of memory These chunks get pushed onto a stack and get popped off when there’s a “return” or the method ends Note that we’ve only been passing primitive types Something different happens with complex types called pass by reference