Invoking methods in the Java library
Download
Report
Transcript Invoking methods in the Java library
Invoking methods in the Java library
Jargon: method invocation
• Terminology:
• Invoking a method = executing a method
• Other phrases with exactly the same meaning:
• calling a method
• running a method
How to invoke a method
• Syntax to invoke a method:
1. Syntax to invoke a method that does not return any
value:
MethodName( parameters ) ; // Runs the
method
How to invoke a method (cont.)
2. Syntax to invoke a method that returns a value:
var = MethodName( parameters ) ;
// Runs the method and when the method
completes,
// stores the value returned by the method
// in the variable "var"
// Note: the data types of the variable and
//
the returned value must matched !!
Input parameters and output values of a
method
• Consider a Mathematical function:
f(x, y, z) = x2 + y3 + z4
Examples:
f(1, 1, 1) = 3
f(2, 2, 1) = 13
Input parameters and output values of a
method (cont.)
• Notes:
• A Mathematical function has a unique name
• A Mathematical function has a number of function
arguments (x, y and z) The Mathematical function uses the
values of the arguments to compute the answer
• A Mathematical function will produce an answer The
answer is the output value of the Mathematical function
Input parameters and output values of a
method (cont.)
• A method resembles a Mathematical function:
• A method has a unique "signature" (will learn more
about "signatures" later in the course)
• A method has a number of input parameters
• A method may produce one output value (some
methods do not produce any output values)
Input parameters and output values of a
method (cont.)
• Facts:
• Mathematical functions are implemented as Java methods
Examples:
• The sine function is implemented as the Math.sin
method in the Java standard library.
• The cosine function is implemented as the
Math.cos method in the Java standard library.
• The square root function is implemented as the
Math.sqrt method in the Java standard library.
Input parameters and output values of a
method (cont.)
• Methods can be used for more than just write
Mathematical functions
After learning Java, you can write methods to solve
problems
Examples:
•You can write a method to find the Greatest
Common Divisor of 2 numbers
•You can write a method to find the best way to
pack boxes of different sizes into a crate
•And so on...
How can you tell what input parameters a method
needs and what kind of value it returns ?
• You need the look at the documentation of the method in
the Java library to find out what input parameters a method
needs and whether the method will return a value
• Website of the Java's documentation:
http://download.oracle.com/javase/6/docs/api/
How can you tell what input parameters a method
needs and what kind of value it returns (cont.) ?
• Suppose we want to learn how to use the Math.sqrt
function
Finding the documentation for the Math.sqrt method:
• The name Math.sqrt means: the sqrt method
inside the Math class
How can you tell what input parameters a method
needs and what kind of value it returns (cont.) ?
•
We must first find the Math class:
• Scroll down the lower left panel to the class Math
and click on the link:
How can you tell what input parameters a method
needs and what kind of value it returns (cont.) ?
• The main window will show the documentation for
the Math class:
How can you tell what input parameters a method
needs and what kind of value it returns (cont.) ?
• Next, we find the sqrt method inside the documentation page
for the Math class:
How can you tell what input parameters a method
needs and what kind of value it returns (cont.) ?
Meaning of the method header:
How can you tell what input parameters a method
needs and what kind of value it returns (cont.) ?
• Example using the Math.sqrt method:
import java.lang.Math; // Not needed... already done by Java compiler
public class Sqrt
{
public static void main(String[] args)
{
double a, b, c;
a = 2.0;
b = Math.sqrt(a);
c = 2.0*Math.sqrt(a) + 1.0;
System.out.print("a = ");
System.out.println(a);
System.out.print("Sqrt of a = ");
System.out.println(b);
System.out.print("2*sqrt(a) + 1 = ");
System.out.println(c);
}
}
What happens when a method is invoked
• Let us examine a portion of the previous example program:
double a, b;
a = 2.0;
b = Math.sqrt(a);
What happens when a method is invoked
(cont.)
• The following figure shows the situation just before the
program executes b = Math.sqrt(a);:
What happens when a method is invoked
(cont.)
• When the method call Math.sqrt(a) is executed, the
computer will do the following:
1.
Pass the value of the variable a as parameter to the
sqrt method:
What happens when a method is invoked
(cont.)
The result is: the sqrt method will use the value 2.0 in the
computations perform by the statements in its method body
2. Execute the statements in the method body of sqrt:
What happens when a method is invoked (cont.)
3.
When it completes, the execution returns to the location of
the method call
The return value replaces the method call in the expression:
What happens when a method is invoked (cont.)
4. After the assigning the value to variable b, we have:
The statement b = Math.sqrt(a); have computed the value √(a)
and stored it inside the variable b.