introducing methods

39
Introducing Methods Introducing Methods Corresponds with Chapter Corresponds with Chapter 5 5

Upload: niabi

Post on 17-Jan-2016

39 views

Category:

Documents


1 download

DESCRIPTION

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 - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Introducing Methods

Introducing MethodsIntroducing Methods

Corresponds with Chapter 5Corresponds with Chapter 5

Page 2: Introducing Methods

What is a Method?What is a Method?

A method is a collection of statements that are grouped together to perform an operation.

Method declaration:Signature

Modifier(s)e.g. public or private, static

Return typee.g. void, int, boolean, etc. OR a class type. OR no type (if a constructor)

IdentifierFormal Parameter List

Enclosed in parenthesestypes and identifiers (like variable declaration)Separated by commas

Method Bodyrequires return statement if return type is not void.Method callSpecify the identifierPlace actual parameters (arguments) in parentheses

Actual data (literal, constant, variable, or expression)

Use return value (if not void)

Page 3: Introducing Methods

Introducing MethodsIntroducing Methods(Example 5.1)(Example 5.1)

method

calling a method

Page 4: Introducing Methods

Anatomy of Method Declaration and Anatomy of Method Declaration and CallCall

method call

modifier

return typeidentifier

formal parameters

method body

return statement

specifyidentifier

Pass actual parameters (arguments)

use return value

method declaraion

Page 5: Introducing Methods

Processing Sequence of a Method Processing Sequence of a Method CallCall

method call

method declaration

2) Pass by value: a num1 is a copy of i, num2 is a copy of j.

3) Function body executes.

4) Return value sent back to calling statement.

5) Return value assigned into k.

1) invoke the method

Page 6: Introducing Methods

Parameter Order and Type Parameter Order and Type AssocationAssocation

void nPrintln (String message, int n)void nPrintln (String message, int n){ { for (int i=0; i<n; i++)for (int i=0; i<n; i++) System.out.println(message);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

Page 7: Introducing Methods

Frame StackFrame Stack The Java Virtual Machine keeps manages the local The Java Virtual Machine keeps manages the local

variables and parameters of method in a Frame Stack (also variables and parameters of method in a Frame Stack (also referred to as Call Stack or Program Stack).referred to as Call Stack or Program Stack). FrameFrame = a data structure that holds the values of all the = a data structure that holds the values of all the

local variables and formal parameters of a method.local variables and formal parameters of a method. StackStack = a last-in, first-out data structure. Items are = a last-in, first-out data structure. Items are

pushed onto the top of the stack. Items are popped off pushed onto the top of the stack. Items are popped off the top of the stack.the top of the stack.

When a method is called, its frame (local variables and When a method is called, its frame (local variables and parameters) are pushed onto the top of the stack.parameters) are pushed onto the top of the stack.

When a method terminates, its frame is removed from When a method terminates, its frame is removed from the stack.the stack. the formal parameters and local variables of a the formal parameters and local variables of a

method exist ONLY AS LONG AS THE METHOD IS method exist ONLY AS LONG AS THE METHOD IS EXECUTING.EXECUTING.

Page 8: Introducing Methods

Memory Changes During Memory Changes During ProcessingProcessing

(Listing 5.1, p132)(Listing 5.1, p132)

Page 9: Introducing Methods

Memory Changes During Memory Changes During ProcessingProcessing(Listing 5.1)(Listing 5.1)

Frame Stack

args

i 5

k

jmain’sframe 2

In main(), before calling max()

Page 10: Introducing Methods

Memory Changes During Memory Changes During ProcessingProcessing(Listing 5.1)(Listing 5.1)

Frame Stack

args

i 5

k

jmain’sframe 2

In max(), just started

max’sframe

num1 5

num2 2

result

Page 11: Introducing Methods

Memory Changes During Memory Changes During ProcessingProcessing(Listing 5.1)(Listing 5.1)

Frame Stack

args

i 5

k

jmain’sframe 2

In max(), before it terminates

max’sframe

num1 5

num2 2

result 5

Page 12: Introducing Methods

Memory Changes During Memory Changes During ProcessingProcessing(Listing 5.1)(Listing 5.1)

Frame Stack

args

i 5

k 55

jmain’sframe 2

Back in main(), after max() returns

NOTE: the value returned from max() was assigned into the variable k.

Page 13: Introducing Methods

DebuggerDebugger

You can view the contents of the You can view the contents of the frame stack in the debugger.frame stack in the debugger.

The Java JDK includes a program for The Java JDK includes a program for debugging applications (called debugging applications (called jdb.exejdb.exe, in the bin subdirectory)., in the bin subdirectory).

NetBeans provides a GUI interface to NetBeans provides a GUI interface to the debugger.the debugger.

NOTE: You will be learning how to use the debugger in future assignments!

Page 14: Introducing Methods

Stopped at this statement (breakpoint)

main’s frame on the frame stack

Local data in main method

Page 15: Introducing Methods

Stopped at this statement (breakpoint)

max’s frame pushed on top of main’s frame

Local data in max method

Page 16: Introducing Methods

Stopped at this statement

(breakpoint)

max’s frame was popped off of the frame stack

max’s return value was assigned to variable k

Page 17: Introducing Methods

ScopeScope A variable’s scope is its visibility.A variable’s scope is its visibility. Which statements can refer to the variable’s identifier.Which statements can refer to the variable’s identifier. Local variables and formal parameters have Local variables and formal parameters have methodmethod

scope. They can only be used inside the method for which scope. They can only be used inside the method for which they are declared.they are declared.

Variables declared inside blocks have block scope. They Variables declared inside blocks have block scope. They can be used only inside the block for which they are can be used only inside the block for which they are declared.declared.

Variables declared in the parentheses of a control Variables declared in the parentheses of a control structure can only be used within the control structure for structure can only be used within the control structure for which they are declared.which they are declared.

You can declare multiple variables of the same name as You can declare multiple variables of the same name as long as they are not in the same nesting structure.long as they are not in the same nesting structure.

You cannot declare variables of the same name within the You cannot declare variables of the same name within the same nesting structure.same nesting structure.

Page 18: Introducing Methods

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.

Page 19: Introducing Methods

Variable result, and parameters num1 and num2 are in max’s scope.

main cannot refer to these identifiers.

Page 20: Introducing Methods

Another Scope ExampleAnother Scope Example

The following example shows The following example shows variables with:variables with: Method scope (available throughout an Method scope (available throughout an

entire method)entire method) Block scope (available only within a Block scope (available only within a

block of code)block of code) Loop scope (available only within a loop)Loop scope (available only within a loop)

Page 21: Introducing Methods

Method scope for method1

Note: the identifiers x and y are not available to the main method.

Page 22: Introducing Methods

Method scope for method2

Page 23: Introducing Methods

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

Page 24: Introducing Methods

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

Page 25: Introducing Methods

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

Page 26: Introducing Methods

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

y

Page 27: Introducing Methods

Method OverloadingMethod Overloading

Overloading = declaring multiple Overloading = declaring multiple methods of the same name, but with methods of the same name, but with different formal parameter lists.different formal parameter lists.

When you call an overloaded When you call an overloaded method, the compiler knows which method, the compiler knows which version you are calling based on the version you are calling based on the types of actual parameters you pass.types of actual parameters you pass.

Page 28: Introducing Methods

Method OverloadingMethod Overloading(Listing 5.4)(Listing 5.4)

Page 29: Introducing Methods

Frame Stack

args

main’sframe

Page 30: Introducing Methods

max’sFrame(int)

num1 3

num2 4

Frame Stack

args

main’sframe

Page 31: Introducing Methods

Frame Stack

args

main’sframe

Page 32: Introducing Methods

max’sFrame(Double)

num1 3.0

num2 5.4

Frame Stack

args

main’sframe

Page 33: Introducing Methods

Frame Stack

args

main’sframe

Page 34: Introducing Methods

max’sFrame(Double,3 params)

num1 3.0

num2 5.4

Frame Stack

args

main’sframe

num3 10.14

Note: here the result returned from a method is the actual parameter of another method call.

Page 35: Introducing Methods

max’sFrame(Double,3 params)

num1 3.0

num2 5.4

Frame Stack

args

main’sframe

num3 10.14

max’sFrame(Double)

num1 3.0

num2 5.4

Page 36: Introducing Methods

max’sFrame(Double,3 params)

num1 3.0

num2 5.4

Frame Stack

args

main’sframe

num3 10.14

5.4

Page 37: Introducing Methods

max’sFrame(Double,3 params)

num1 3.0

num2 5.4

Frame Stack

args

main’sframe

num3 10.14

max’sFrame(Double)

num1 5.4

num2 10.14

Page 38: Introducing Methods

max’sFrame(Double,3 params)

num1 3.0

num2 5.4

Frame Stack

args

main’sframe

num3 10.14

10.14

Page 39: Introducing Methods

Frame Stack

args

main’sframe