The life time of local variables (in a method). Local variables Local variable: A local variable is used to store information that is relevant for the.

Slides:



Advertisements
Similar presentations
Designing a Program & the Java Programming Language
Advertisements

1 Storage Duration and Scope –Local and global variables Storage classes –automatic, static, external, register Todays Material.
Parameter passing mechanism: pass-by-reference. The Pass-by-reference mechanism - the agreement Recall: Parameter passing mechanism = agreement between.
Introduction to Programming G51PRG University of Nottingham Revision 1
Classes  All code in a Java program is part of a class  A class has two purposes  Provide functions to do work for the programmer  Represent data.
Dale Roberts Introduction to Java - First Program Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer and.
Lecture 2 Introduction to C Programming
 2005 Pearson Education, Inc. All rights reserved Introduction.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Principles of Programming Fundamental of C Programming Language and Basic Input/Output Function 1.
Numeric literals and named constants. Numeric literals Numeric literal: Example: A numeric literal is a constant value that appears in a Java program.
Slides prepared by Rose Williams, Binghamton University Chapter 11 Recursion.
CMT Programming Software Applications
Supplementary for method, DCO10803, Quarter 3, Page 1 of 7 Object-Oriented Programming and Design DCO10803 Supplementary for method  Prototype.
Writing algorithms using the while-statement. Previously discussed Syntax of while-statement:
The different kinds of variables in a Java program.
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.
The different types of variables in a Java program.
Programming a computer. What does programming a computer mean ? Programming a computer: Since a computer can only execute machine instructions (encoded.
11 Chapter 4 LOOPS AND FILES. 22 THE INCREMENT AND DECREMENT OPERATORS To increment a variable means to increase its value by one. To decrement a variable.
Using Java's Math & Scanner class. Java's Mathematical functions (methods) (1)
COMP More About Classes Yi Hong May 22, 2015.
By: Mr. Baha Hanene Chapter 3. Learning Outcomes We will cover the learning outcome 02 in this chapter i.e. Use basic data-types and input / output in.
Introducing Java.
Floating point variables of different lengths. Trade-off: accuracy vs. memory space Recall that the computer can combine adjacent bytes in the RAM memory.
Memory organization - storing variables efficiently in the RAM memory.
IT253: Computer Organization Lecture 4: Instruction Set Architecture Tonga Institute of Higher Education.
Week 3 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
The string data type String. String (in general) A string is a sequence of characters enclosed between the double quotes "..." Example: Each character.
The Rectangle Method. Introduction Definite integral (High School material): A definite integral a ∫ b f(x) dx is the integral of a function f(x) with.
CSC204 – Programming I Lecture 4 August 28, 2002.
Parameter passing mechanism: pass-by-value. Introduction In the last webpage, we discussed how to pass information to a method I have kept it (deliberately)
Topic 1Topic 2Topic 3Topic 4Topic
The basics of the array data structure. Storing information Computer programs (and humans) cannot operate without information. Example: The array data.
Floating point numerical information. Previously discussed Recall that: A byte is a memory cell consisting of 8 switches and can store a binary number.
What does a computer program look like: a general overview.
The scope of local variables. Murphy's Law The famous Murphy's Law says: Anything that can possibly go wrong, does. (Wikipedia page on Murphy's Law:
Introduction to C Programming Angela Chih-Wei Tang ( 唐 之 瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan 2010 Fall.
Introduction to programming in the Java programming language.
Assignment statements using the same variable in LHS and RHS.
Mixing integer and floating point numbers in an arithmetic operation.
Introduction to Methods. Previously discussed There are similarities in make up of that can help you remember the construct of a class a class in the.
Classes: user-defined types. Organizing method with a class A class is used to organize methods * Methods that compute Mathematical functions * The Scanner.
The assignment expressions. The assignment operator in an assignment statement We have seen the assignment statement: Effect: var = expr; Stores the value.
The while-statement. The loop statements in Java What is a loop-statement: A loop-statement is a statement that repeatedly executes statements contained.
CPU Control Unit Main memory ALU INPUTINPUT OUTPUTOUTPUT ฯ OS Compiler OS   Compiler.
Reading input from the console input. Java's console input The console is the terminal window that is running the Java program I.e., that's the terminal.
Introduction to array: why use arrays ?. Motivational example Problem: Write a program that reads in and stores away 5 double numbers After reading in.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 1.
Simple algorithms on an array - compute sum and min.
1 Overview of Programming Principles of Computers.
Methods.
Computer Science A 1. Course plan Introduction to programming Basic concepts of typical programming languages. Tools: compiler, editor, integrated editor,
Invoking methods in the Java library. Jargon: method invocation Terminology: Invoking a method = executing a method Other phrases with exactly the same.
Chapter 7 - Functions. Functions u Code group that performs single task u Specification refers to what goes into and out of function u Design refers to.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 1: Introduction to Computers and Programming.
History of C and basics of Programming
Information and Computer Sciences University of Hawaii, Manoa
Course Contents KIIT UNIVERSITY Sr # Major and Detailed Coverage Area
User-Written Functions
CSC201: Computer Programming
AKA the birth, life, and death of variables.
Writing Methods.
The method invocation mechanism and the System Stack
The Boolean (logical) data type boolean
Lecture Notes - Week 2 Lecture-1. Lecture Notes - Week 2 Lecture-1.
Local variables and how to recognize them
LOOPS The loop is the control structure we use to specify that a statement or group of statements is to be repeatedly executed. Java provides three kinds.
Computer Programming-1 CSC 111
Introduction to Computer Science and Object-Oriented Programming
Presentation transcript:

The life time of local variables (in a method)

Local variables Local variable: A local variable is used to store information that is relevant for the duration of the execution of one method

Local variables (cont.) How to the define a local variable: A local variable is defined inside the body of a method (I.e., a local variable is defined between the opening and closing braces of a method)

Local variables (cont.) Example: public class MyProgram { public static void main(String[] args) { // Body of method "main" double r; // *** Local variable !!! r = MyProgran.min( 1.0, 4.0 ); System.out.println(r); r = MyProgram.min( 3.7, -2.9 ); System.out.println(r); r = MyProgram.min( -9.9, 3.8 ); System.out.println(r); }

Local variables (cont.) public class ToolBox { public static double min ( double a, double b ) { // Body of method "min" double m = 0; // *** Local variable !!! if ( a < b ) { m = a; // a is the smaller value } else { m = b; // b is the smaller value } return(m); }

Local variables (cont.) Next, we study the life time and the scoping rules for local variables

The life time of variables Kind of like animals, variables in a computer program has a life time: A variable is created ("born") at a certain moment It exists for some time while the computer program is executing.... And then the variable ceases to exist

The life time of variables (cont.) Previously discussed: A variable in a running computer program is in fact a memory cell (See: 4/float.html#variable ) Creating a variable = reserve memory space for a variable (See: 4/float.html#create-variable)

The life time of variables (cont.) Computer Science Jargon: Destroying a variable = reclaim the reserved memory space occupied by a variable When the reserved memory space taken up by a variable is reclaimed, that variable will cease to exist

The life time of variables (cont.) Definition: the life time of a variable: Life time of a variable = the duration between the moment when a variable is created and the moment when a variable is destroyed

The life time of a local variable Moment of creation (of a local variable): A local variable is created at the moment that the program execution reaches the definition of the local variable

The life time of a local variable Moment of destruction (of a local variable): A local variable is destroyed at the moment that the program execution reaches the end of the method in which the local variable is defined

Example: how local variables are created and destroyed Consider the following program:

Example: how local variables are created and destroyed (cont.) When the program starts to run, the RAM memory contains only the program instructions:

Example: how local variables are created and destroyed (cont.) The program starts to run with the main method. The current program location is given by a green arrow:

Example: how local variables are created and destroyed (cont.)

Note: There are 2 dark green arrows in the figure. The arrow in the left figure points to the current program location in the human readable Java program The Java program must be compiled (translated) into machine instruction to be executed. The Java program is given to help you follow what the computer will do

Example: how local variables are created and destroyed (cont.) The arrow in the right figure points to the current program location in the machine instructions program The machine instruction program is the actual program that is executed by the computer !!!

Example: how local variables are created and destroyed (cont.) Here is how the local variables are created and destroyed: When the computer encounters the definition of the local variable r, it creates it in RAM (in other words: reserve memory to hold the value)

Example: how local variables are created and destroyed (cont.)

When the computer executes the method call, the program execution is transferred, to the min method:

Example: how local variables are created and destroyed (cont.) When the computer encounters the definition of the local variable m, it creates it in RAM (in other words: reserve memory to hold the value)

Example: how local variables are created and destroyed (cont.) The program execution continues to compute the minimum of the input values and store it in the local variable m:

Example: how local variables are created and destroyed (cont.) The return(m) statement will save the value of variable m in a return location:

Example: how local variables are created and destroyed (cont.) The return location is typically a general purpose register inside the CPU (Central Processing Unit) (CPU, see: -computer2.html#CPU )

Example: how local variables are created and destroyed (cont.) When program execution reaches the end of the function, the local variable m is destroyed:

Example: how local variables are created and destroyed (cont.) Furthermore, the program execution will be transferred back to the location of the method call.

Example: how local variables are created and destroyed (cont.) When program execution continues the location of the method call, the assignment statement will stored the value in the return location to the local variable r:

Example: how local variables are created and destroyed (cont.) And when the program reaches the end of the main method, the local variable r will also be destroyed:

Example: how local variables are created and destroyed (cont.) Conclusion: A local variable only exists between: The definition of the local variable The end of the method where the local variable is defined

Example: how local variables are created and destroyed (cont.) Example 1: The local variable m only exists within this green area:

Example: how local variables are created and destroyed (cont.) Example 2: The local variable r only exists within this green area: