Corresponds with Chapter 5

Slides:



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

1 Classes, Encapsulation, Methods and Constructors (Continued) Class definitions Instance data Encapsulation and Java modifiers Method declaration and.
Chapter 7: User-Defined Functions II
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 6 Functions.
Lecture 3: Topics If-then-else Operator precedence While loops Static methods Recursion.
COMP 14 Introduction to Programming Miguel A. Otaduy May 25, 2004.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 8, 2005.
Chapter 4 Methods F Introducing Methods –Benefits of methods, Declaring Methods, and Calling Methods F Passing Parameters –Pass by Value F Overloading.
1 Chapter 5 Methods. 2 Introducing Methods A method is a collection of statements that are grouped together to perform an operation.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 7 User-Defined Methods.
Lesson 6 Functions Also called Methods CS 1 Lesson 6 -- John Cole1.
Chapter 7: User-Defined Methods
Chapter 6: Function. Scope of Variable A scope is a region of the program and broadly speaking there are three places, where variables can be declared:
Chapter 6: Functions.
Java Methods By J. W. Rider. Java Methods Modularity Declaring methods –Header, signature, prototype Static Void Local variables –this Return Reentrancy.
COMP More About Classes Yi Hong May 22, 2015.
More with Methods (parameters, reference vs. value, array processing) Corresponds with Chapters 5 and 6.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 6 Functions.
Copyright © 2012 Pearson Education, Inc. Chapter 6: Functions.
1 Chapter 5 - General Procedures 5.1 Function Procedures 5.2 Sub Procedures, Part I 5.3 Sub Procedures, Part II 5.4 Modular Design.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
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.
Copyright © 2012 Pearson Education, Inc. Chapter 6: Functions.
Methods F Hello World! F Java program compilation F Introducing Methods F Declaring Methods F Calling Methods F Passing Parameters by value F Overloading.
User Defined Functions Chapter 7 2 Chapter Topics Void Functions Without Parameters Void Functions With Parameters Reference Parameters Value and Reference.
1 Methods Introduction to Methods Passing Arguments to a Method More About Local Variables Returning a Value from a Method Problem Solving with Methods.
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.
1 Brief Version of Starting Out with C++, 4th Brief Edition Chapter 6 Functions.
CIS 234: Java Methods Dr. Ralph D. Westfall April, 2010.
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.
User Defined Methods Methods are used to divide complicated programs into manageable pieces. There are predefined methods (methods that are already provided.
Chapter Functions 6. Modular Programming 6.1 Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
1 Static Variable and Method Lecture 9 by Dr. Norazah Yusof.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 6: Functions.
Chapter 5 Methods 1. Motivations Method : groups statements that perform a function.  Level of abstraction (black box)  Code Reuse – no need to reinvent.
 Static  Example for Static Field  Example for Static Method  Math class methods  Casting  Scope of Declaration  Method Overloading  Constructor.
AP Java Ch. 4 Review Question 1  Java methods can return only primitive types (int, double, boolean, etc).
Methods main concepts – method call – object 's methods – actual parameters – method declaration – formal parameters – return value other concepts – method.
Lecture 4 – Function (Part 1) FTMK, UTeM – Sem /2014.
Chapter 6 Functions. 6-2 Topics 6.1 Modular Programming 6.2 Defining and Calling Functions 6.3 Function Prototypes 6.4 Sending Data into a Function 6.5.
(C) 2010 Pearson Education, Inc. All rights reserved.  Best way to develop and maintain a large program is to construct it from small, simple pieces,
Tarik Booker CS 242. What we will cover…  Functions  Function Syntax  Local Variables  Global Variables  The Scope of Variables  Making Functions.
Problem of the Day  Why are manhole covers round?
 Pearson Education, Inc. All rights reserved Methods: A Deeper Look.
Information and Computer Sciences University of Hawaii, Manoa
Java Memory Management
Chapter 7 User-Defined Methods.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 7 User-Defined Methods.
Chapter 7: User-Defined Functions II
Java Memory Management
Java Programming: Guided Learning with Early Objects
Object Oriented Systems Lecture 03 Method
Chapter 6: Functions Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Methods.
Chapter 6 Methods: A Deeper Look
6 Chapter Functions.
Group Status Project Status.
Classes, Encapsulation, Methods and Constructors (Continued)
CS2011 Introduction to Programming I Methods (II)
Chapter 6 Methods.
BBIT 212/ CISY 111 Object Oriented Programming (OOP)
6 Methods: A Deeper Look.
Object Oriented Programming in java
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Standard Version of Starting Out with C++, 4th Edition
Chapter 6: Methods CS1: Java Programming Colorado State University
Presentation transcript:

Corresponds with Chapter 5 Introducing Methods Corresponds with Chapter 5

What is a Method? Method declaration: Signature Modifier(s) e.g. public or private, static Return type e.g. void, int, boolean, etc. OR a class type. OR no type (if a constructor) Identifier Formal Parameter List Enclosed in parentheses types and identifiers (like variable declaration) Separated by commas Method Body requires return statement if return type is not void. A method is a collection of statements that are grouped together to perform an operation. Method call Specify the identifier Place actual parameters (arguments) in parentheses Actual data (literal, constant, variable, or expression) Use return value (if not void)

Introducing Methods (Example 5.1) calling a method method

Anatomy of Method Declaration and Call method call Pass actual parameters (arguments) use return value specify identifier identifier return type formal parameters modifier method body return statement method declaraion

Processing Sequence of a Method Call 1) invoke the method 5) Return value assigned into k. 2) Pass by value: a num1 is a copy of i, num2 is a copy of j. 4) Return value sent back to calling statement. 3) Function body executes. method declaration

Parameter Order and Type Assocation void nPrintln (String message, int n) { for (int i=0; i<n; i++) System.out.println(message); } IMPORTANT: the order and data type of actual parameters in a method call MUST match the order and data type of formal parameters in the method signature. Otherwise you will get a syntax error. nPrintln(“Hello there”, 100); nPrintln(100, “Hello there”); OK Not OK

Frame Stack The Java Virtual Machine keeps manages the local variables and parameters of method in a Frame Stack (also referred to as Call Stack or Program Stack). Frame = a data structure that holds the values of all the local variables and formal parameters of a method. Stack = a last-in, first-out data structure. Items are pushed onto the top of the stack. Items are popped off the top of the stack. When a method is called, its frame (local variables and parameters) are pushed onto the top of the stack. When a method terminates, its frame is removed from the stack.  the formal parameters and local variables of a method exist ONLY AS LONG AS THE METHOD IS EXECUTING.

Memory Changes During Processing (Listing 5.1, p132)

Memory Changes During Processing (Listing 5.1) In main(), before calling max() main’s frame args i 5 j 2 k Frame Stack

Memory Changes During Processing (Listing 5.1) In max(), just started max’s frame num1 5 num2 2 result main’s frame args i 5 j 2 k Frame Stack

Memory Changes During Processing (Listing 5.1) In max(), before it terminates max’s frame num1 5 num2 2 result 5 main’s frame args i 5 j 2 k Frame Stack

Memory Changes During Processing (Listing 5.1) Back in main(), after max() returns NOTE: the value returned from max() was assigned into the variable k. main’s frame args i 5 j 2 k 5 Frame Stack

Debugger You can view the contents of the frame stack in the debugger. The Java JDK includes a program for debugging applications (called jdb.exe, in the bin subdirectory). NetBeans provides a GUI interface to the debugger. NOTE: You will be learning how to use the debugger in future assignments!

Stopped at this statement (breakpoint) main’s frame on the frame stack Local data in main method

Stopped at this statement (breakpoint) max’s frame pushed on top of main’s frame Local data in max method

Stopped at this statement (breakpoint) max’s return value was assigned to variable k max’s frame was popped off of the frame stack

Scope A variable’s scope is its visibility. Which statements can refer to the variable’s identifier. Local variables and formal parameters have method scope. They can only be used inside the method for which they are declared. Variables declared inside blocks have block scope. They can be used only inside the block for which they are declared. Variables declared in the parentheses of a control structure can only be used within the control structure for which they are declared. You can declare multiple variables of the same name as long as they are not in the same nesting structure. You cannot declare variables of the same name within the same nesting structure.

Variables i, j, and k, and parameter args are in main’s scope. Note: max cannot refer to any of main’s variables or parameters...otherwise you’ll get a syntax error.

Variable result, and parameters num1 and num2 are in max’s scope. main cannot refer to these identifiers.

Another Scope Example The following example shows variables with: Method scope (available throughout an entire method) Block scope (available only within a block of code) Loop scope (available only within a loop)

Method scope for method1 Note: the identifiers x and y are not available to the main method.

Method scope for method2

i is available within this loop. z is available within the block

i is available within this loop. z is available within the block

Identical variable names for different variables Identical variable names for different variables. OK because different blocks, not one nested in the other

Identical variable names for different variables Identical variable names for different variables. NOT OK because the loop block is nested inside the method. This will cause a syntax error. y

Method Overloading Overloading = declaring multiple methods of the same name, but with different formal parameter lists. When you call an overloaded method, the compiler knows which version you are calling based on the types of actual parameters you pass.

Method Overloading (Listing 5.4)

main’s frame args Frame Stack

num1 3 max’s Frame (int) num2 4 main’s frame args Frame Stack

main’s frame args Frame Stack

num1 3.0 max’s Frame (Double) num2 5.4 main’s frame args Frame Stack

main’s frame args Frame Stack

Note: here the result returned from a method is the actual parameter of another method call. num1 3.0 max’s Frame (Double, 3 params) num2 5.4 num3 10.14 main’s frame args Frame Stack

Frame Stack num1 3.0 max’s Frame num2 5.4 num1 3.0 max’s Frame num2 (Double) num2 5.4 num1 3.0 max’s Frame (Double, 3 params) num2 5.4 num3 10.14 main’s frame args Frame Stack

Frame Stack num1 3.0 max’s Frame num2 5.4 num3 10.14 main’s frame args (Double, 3 params) num2 5.4 num3 10.14 main’s frame args 5.4 Frame Stack

Frame Stack num1 5.4 max’s Frame num2 10.14 num1 3.0 max’s Frame num2 (Double) num2 10.14 num1 3.0 max’s Frame (Double, 3 params) num2 5.4 num3 10.14 main’s frame args Frame Stack

Frame Stack num1 3.0 max’s Frame num2 5.4 num3 10.14 main’s frame args (Double, 3 params) num2 5.4 num3 10.14 main’s frame args 10.14 Frame Stack

main’s frame args Frame Stack