introducing methods
Download
Skip this Video
Download Presentation
Introducing Methods

Loading in 2 Seconds...

play fullscreen
1 / 39

Introducing Methods - PowerPoint PPT Presentation


  • 93 Views
  • Uploaded on

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

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about ' Introducing Methods' - niabi


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
introducing methods

Introducing Methods

Corresponds with Chapter 5

what is a method
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
Introducing Methods(Example 5.1)

calling a method

method

anatomy of method declaration and call

Pass actual parameters (arguments)

method call

identifier

specify

identifier

use return value

return type

formal parameters

modifier

method body

return statement

Anatomy of Method Declaration and Call

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 ofi, num2 is a copy of j.

4) Return value sent back to calling statement.

Processing Sequence of a Method Call

method call

3) Function body executes.

method declaration

parameter order and type assocation

nPrintln(“Hello there”, 100);

nPrintln(100, “Hello there”);

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.

OK

Not OK

frame stack
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

args

main’s

frame

5

i

k

j

Memory Changes During Processing(Listing 5.1)

In main(), before calling max()

2

Frame Stack

memory changes during processing listing 5 11

5

2

num1

num2

result

args

main’s

frame

max’s

frame

5

i

k

j

Memory Changes During Processing(Listing 5.1)

In max(), just started

2

Frame Stack

memory changes during processing listing 5 12

2

5

result

num1

num2

args

main’s

frame

max’s

frame

5

i

k

j

Memory Changes During Processing(Listing 5.1)

In max(), before it terminates

5

2

Frame Stack

memory changes during processing listing 5 13

args

main’s

frame

5

5

i

k

j

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.

2

Frame Stack

debugger
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!

slide14

Stopped at this statement (breakpoint)

main’s frame on the frame stack

Local data in main method

slide15

Stopped at this statement (breakpoint)

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

Local data in max method

slide16

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
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.
slide18

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.

slide19

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

main cannot refer to these identifiers.

another scope example
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)
slide21

Method scope for method1

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

slide25

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

slide26

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
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.
slide29

args

main’s

frame

Frame Stack

slide30

3

4

num1

num2

args

main’s

frame

max’s

Frame

(int)

Frame Stack

slide31

args

main’s

frame

Frame Stack

slide32

3.0

5.4

num1

num2

args

main’s

frame

max’s

Frame

(Double)

Frame Stack

slide33

args

main’s

frame

Frame Stack

slide34

3.0

5.4

10.14

num1

num2

num3

args

main’s

frame

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

max’s

Frame

(Double,

3 params)

Frame Stack

slide35

3.0

5.4

10.14

3.0

5.4

num1

num2

num3

num1

num2

args

main’s

frame

max’s

Frame

(Double)

max’s

Frame

(Double,

3 params)

Frame Stack

slide36

3.0

5.4

10.14

num1

num2

num3

args

main’s

frame

max’s

Frame

(Double,

3 params)

5.4

Frame Stack

slide37

3.0

5.4

10.14

5.4

10.14

num1

num2

num3

num1

num2

args

main’s

frame

max’s

Frame

(Double)

max’s

Frame

(Double,

3 params)

Frame Stack

slide38

3.0

5.4

10.14

num1

num2

num3

args

main’s

frame

max’s

Frame

(Double,

3 params)

Frame Stack

10.14

slide39

args

main’s

frame

Frame Stack

ad