Local variables and how to recognize them

Slides:



Advertisements
Similar presentations
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.
Advertisements

Dale Roberts Introduction to Java - First Program Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer and.
5/17/ Programming Constructs... There are several types of programming constructs in JAVA. - If-else construct or ternary operator - while - do-while.
Introduction to Application Programming IST 256 Application Programming for Information Systems Xiaozhong Liu
1 What’s a class People are confused with nested classes and anonymous classes. Experiments in a recitation last week revealed that the problems were probably.
Supplementary for method, DCO10803, Quarter 3, Page 1 of 7 Object-Oriented Programming and Design DCO10803 Supplementary for method  Prototype.
Writing Methods. Create the method Methods, like functions, do something They contain the code that performs the job Methods have two parts.
Methods & Activation Record. Recap: what’s a method?
Introduction to Methods
The different kinds of variables in a Java program.
The different types of variables in a Java program.
Unit 2: Java Introduction to Programming 2.1 Initial Example.
COMP More About Classes Yi Hong May 22, 2015.
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.
The Java Programming Language
Classes and Methods Computer Engineering Department Java Course Asst. Prof. Dr. Ahmet Sayar Kocaeli University - Fall 2014.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Functions.
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)
 2005 Pearson Education, Inc. All rights reserved. 1 Methods Called functions or procedures in other languages Modularize programs by separating its tasks.
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:
Working with arrays (we will use an array of double as example)
The if StatementtMyn1 The if Statement The basic if statement allows your program to execute a single statement, or a block of statements enclosed between.
Procedural Programming Criteria: P2 Task: 1.2 Thomas Jazwinski.
Writing Static Methods Up until now, we have been USING (calling) static methods that other people have written. Now, we will start CREATING our own static.
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.
The Bisection Method. Introduction Bisection Method: Bisection Method = a numerical method in Mathematics to find a root of a given function.
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.
Methods. Methods also known as functions or procedures. Methods are a way of capturing a sequence of computational steps into a reusable unit. Methods.
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.
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.
Simple algorithms on an array - compute sum and min.
+ Storage Classes and Linkage. + Introduction Scope describe the region or regions of a program that can access and identifier Variables can be shared.
Chapter 5 Methods 1. Motivations Method : groups statements that perform a function.  Level of abstraction (black box)  Code Reuse – no need to reinvent.
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.
Recursion. Recursive Methods  A recursive method is a method that calls itself.  General Form of Simple Recursive Methods  Every recursive method has.
Software development For large and complex software use divide and conquer rule. Software design methods: Structured design Object-Oriented design.
Methods.
COP 2220 Computer Science I Topics –Breaking Problems Down –Functions –User-defined Functions –Calling Functions –Variable Scope Lecture 4.
Lecture 6: Methods MIT-AITI Kenya © 2005 MIT-Africa Internet Technology Initiative In this lecture, you will learn… What a method is Why we use.
Invoking methods in the Java library. Jargon: method invocation Terminology: Invoking a method = executing a method Other phrases with exactly the same.
JAVA METHODS (FUNCTIONS). Why are they called methods? Java is a strictly object-oriented programming language Methods are functions inside of objects.
Methods Matthew Harrison. Overview ● There are five main aspects of methods... ● 1) Modifiers – public, private ● 2) Method Name ● 3) Parameters ● 4)
GC211 Data structure Lecture 3 Sara Alhajjam.
Java Memory Management
Java Memory Management
The Lifetime of a Variable
AKA the birth, life, and death of variables.
Department of Computer Science
C Functions -Continue…-.
JavaScript: Functions
CS/ENGRD 2110 Spring 2018 Lecture 5: Local vars; Inside-out rule; constructors
Scope, Parameter Passing, Storage Specifiers
CS/ENGRD 2110 Spring 2017 Lecture 5: Local vars; Inside-out rule; constructors
Writing Methods.
Stack Memory 2 (also called Call Stack)
The Boolean (logical) data type boolean
Introducing Arrays Array is a data structure that represents a collection of the same types of data.
CS/ENGRD 2110 Fall 2017 Lecture 5: Local vars; Inside-out rule; constructors
Subprograms Functions.
CIS 110: Introduction to Computer Programming
Recursive GCD Demo public class Euclid {
CS/ENGRD 2110 Fall 2018 Lecture 5: Local vars; Inside-out rule; constructors
AKA the birth, life, and death of variables.
class PrintOnetoTen { public static void main(String args[]) {
Lecture Notes - Week 2 Lecture-1. Lecture Notes - Week 2 Lecture-1.
Scope of variables class scopeofvars {
Recursion Method calling itself (circular definition)
A Methodical Approach to Methods
More on iterations using
Presentation transcript:

Local variables and how to recognize them

Purpose of local variables A local variable will exists as long as the method in which they have been been created is still running As soon as the method terminates (i.e., returns), all the local variables defined inside the method are destroyed. A local variable is used to store information that is relevant for the duration of the execution of one method

Defining (recognizing) local variables 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)

Defining (recognizing) 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 ); r = MyProgram.min( -9.9, 3.8 ); }

Defining (recognizing) 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);

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