|
|
E.g.: double
public class ToolBox // Methods must be contained inside a class
{
/* ----------------------------------------
A method called "min" that contains
the statements to find the smaller
of 2 values a and b
--------------------------------------- */
public static double min ( double a, double b )
{
double m = 0;
if ( a < b )
{
m = a; // a is the smaller value
}
else
{
m = b; // b is the smaller value
}
return(m); // Return value of the method
}
}
|
double x; x = ToolBox.min(4.5, 8.9); |
When ToolBox.min(4.5, 8.9) returns, the method invocation is replace by the return value.
In other words:
x = ToolBox.min(4.5, 8.9); Becomes: x = 4.5; |
|
Java program that returns an array:
public static double[] returnArray( )
{
double[] x;
x = new double[3]; // Create an array of 3 elements
x[0] = 2.3;
x[1] = 3.4;
x[2] = 4.5;
return( x ); // Return the **reference** (location) of the array
}
|
Explanation:
|
|
Example:
public static void main(String[] args)
{
double[] a; // "a" can contain a ref. to an array of double
a = returnArray(); // "a" can receive the return value !!!
System.out.println("Array AFTER calling returnArray:");
for (int i = 0; i < a.length; i++)
System.out.println( a[i] h );
}
|
How to run the program:
|