Download presentation
Presentation is loading. Please wait.
Published byOswin Watson Modified over 8 years ago
1
1992-2007 Pearson Education, Inc. All rights reserved. 1 6 6 Methods: A Deeper Look
2
1992-2007 Pearson Education, Inc. All rights reserved. 2 6.1 Introduction 6.2 Program Modules in Java 6.3 static Methods, static Fields and Class Math 6.4 Declaring Methods with Multiple Parameters 6.5 Notes on Declaring and Using Methods 6.6 Method Call Stack and Activation Records 6.7 Argument Promotion and Casting 6.8 Java API Packages 6.9 Case Study: Random-Number Generation 6.9.1 Generalized Scaling and Shifting of Random Numbers 6.9.2 Random-Number Repeatability for Testing and Debugging
3
1992-2007 Pearson Education, Inc. All rights reserved. 3 6.1 Introduction Divide and conquer technique – Construct a large program from smaller pieces (or modules) – Can be accomplished using methods static methods can be called without the need for an object of the class Random number generation Constants
4
1992-2007 Pearson Education, Inc. All rights reserved. 4 6.2 Program Modules in Java Java Application Programming Interface (API) – Also known as the Java Class Library – Contains predefined methods and classes Related classes are organized into packages Includes methods for mathematics, string/character manipulations, input/output, databases, networking, file processing, error checking and more
5
1992-2007 Pearson Education, Inc. All rights reserved. 5 6.2 Program Modules in Java (Cont.) Methods – Called functions or procedures in some other languages – Modularize programs by separating its tasks into self- contained units – Enable a divide-and-conquer approach – Are reusable in later programs – Prevent repeating code
6
1992-2007 Pearson Education, Inc. All rights reserved. 6 6.3 static Methods, static Fields and Class Math static method (or class method) – Applies to the class as a whole instead of a specific object of the class – Call a static method by using the method call: ClassName. methodName ( arguments ) – All methods of the Math class are static example: Math.sqrt( 900.0 )
7
1992-2007 Pearson Education, Inc. All rights reserved. 7 Fig. 6.1 | Hierarchical boss-method/worker-method relationship.
8
1992-2007 Pearson Education, Inc. All rights reserved. 8 6.3 static Methods, static Fields and Class Math (Cont.) Constants – Keyword final – Cannot be changed after initialization static fields (or class variables) – Are fields where one copy of the variable is shared among all objects of the class Math.PI and Math.E are final static fields of the Math class
9
1992-2007 Pearson Education, Inc. All rights reserved. 9 Fig. 6.2 | Math class methods.
10
1992-2007 Pearson Education, Inc. All rights reserved. 10 6.3 static Methods, static Fields and Class Math (Cont.) Method main – main is declared static so it can be invoked without creating an object of the class containing main – Any class can contain a main method The JVM invokes the main method belonging to the class specified by the first command-line argument to the java command
11
1992-2007 Pearson Education, Inc. All rights reserved. 11 6.4 Declaring Methods with Multiple Parameters Multiple parameters can be declared by specifying a comma-separated list. – Arguments passed in a method call must be consistent with the number, types and order of the parameters Sometimes called formal parameters
12
1992-2007 Pearson Education, Inc. All rights reserved. 12 Outline MaximumFinder.java (1 of 2) Call method maximum Display maximum value
13
1992-2007 Pearson Education, Inc. All rights reserved. 13 Outline MaximumFinder.java (2 of 2) Declare the maximum method Compare y and maximumValue Compare z and maximumValue Return the maximum value
14
1992-2007 Pearson Education, Inc. All rights reserved. 14 Outline MaximumFinderTest.java Create a MaximumFinder object Call the determineMaximum method
15
1992-2007 Pearson Education, Inc. All rights reserved. 15 6.5 Notes on Declaring and Using Methods Three ways to call a method: – Use a method name by itself to call another method of the same class – Use a variable containing a reference to an object, followed by a dot (. ) and the method name to call a method of the referenced object – Use the class name and a dot (. ) to call a static method of a class static methods cannot call non- static methods of the same class directly
16
1992-2007 Pearson Education, Inc. All rights reserved. 16 6.5 Notes on Declaring and Using Methods (Cont.) Three ways to return control to the calling statement: – If method does not return a result: Program flow reaches the method-ending right brace or Program executes the statement return; – If method does return a result: Program executes the statement return expression ; – expression is first evaluated and then its value is returned to the caller
17
1992-2007 Pearson Education, Inc. All rights reserved. 17 6.6 Method Call Stack and Activation Records Stacks – Last-in, first-out (LIFO) data structures Items are pushed (inserted) onto the top Items are popped (removed) from the top Program execution stack – Also known as the method call stack – Return addresses of calling methods are pushed onto this stack when they call other methods and popped off when control returns to them
18
1992-2007 Pearson Education, Inc. All rights reserved. 18 6.6 Method Call Stack and Activation Records (Cont.) – A method’s local variables are stored in a portion of this stack known as the method’s activation record or stack frame When the last variable referencing a certain object is popped off this stack, that object is no longer accessible by the program – Will eventually be deleted from memory during “garbage collection” Stack overflow occurs when the stack cannot allocate enough space for a method’s activation record
19
1992-2007 Pearson Education, Inc. All rights reserved. 19 6.7 Argument Promotion and Casting Argument promotion – Java will promote a method call argument to match its corresponding method parameter according to the promotion rules – Values in an expression are promoted to the “highest” type in the expression (a temporary copy of the value is made) – Converting values to lower types results in a compilation error, unless the programmer explicitly forces the conversion to occur Place the desired data type in parentheses before the value – example: ( int ) 4.5
20
1992-2007 Pearson Education, Inc. All rights reserved. 20 Fig. 6.5 | Promotions allowed for primitive types.
21
1992-2007 Pearson Education, Inc. All rights reserved. 21 6.11 Scope of Declarations Basic scope rules – Scope of a parameter declaration is the body of the method in which appears – Scope of a local-variable declaration is from the point of declaration to the end of that block – Scope of a local-variable declaration in the initialization section of a for header is the rest of the for header and the body of the for statement – Scope of a method or field of a class is the entire body of the class
22
1992-2007 Pearson Education, Inc. All rights reserved. 22 6.11 Scope of Declarations (Cont.) Shadowing – A field is shadowed (or hidden) if a local variable or parameter has the same name as the field This lasts until the local variable or parameter goes out of scope
23
1992-2007 Pearson Education, Inc. All rights reserved. 23 Outline Scope.java (1 of 2) Shadows field x Display value of local variable x
24
1992-2007 Pearson Education, Inc. All rights reserved. 24 Outline Scope.java (2 of 2) Shadows field x Display value of local variable x Display value of field x
25
1992-2007 Pearson Education, Inc. All rights reserved. 25 Outline ScopeTest.java
26
1992-2007 Pearson Education, Inc. All rights reserved. 26 6.12 Method Overloading Method overloading – Multiple methods with the same name, but different types, number or order of parameters in their parameter lists – Compiler decides which method is being called by matching the method call’s argument list to one of the overloaded methods’ parameter lists A method’s name and number, type and order of its parameters form its signature – Differences in return type are irrelevant in method overloading Overloaded methods can have different return types Methods with different return types but the same signature cause a compilation error
27
1992-2007 Pearson Education, Inc. All rights reserved. 27 Outline MethodOverload.java Correctly calls the “ square of int ” method Correctly calls the “ square of double ” method Declaring the “ square of int ” method Declaring the “ square of double ” method
28
1992-2007 Pearson Education, Inc. All rights reserved. 28 Outline MethodOverloadTest.java
29
1992-2007 Pearson Education, Inc. All rights reserved. 29 Outline MethodOverload Error.java Same method signature Compilation error
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.