The content below assumes familiarity with Writing and calling methods and Methods with parameters.
A Java method can return 0 or 1 value to the code that called the method. A method with a return value can be called by other code and the return value used by the calling code.
findSmallest
method with println
statement
public static void findSmallest(int a, int b, int c)
{
int smallest = a;
if(b < smallest)
smallest = b;
if(c < smallest)
smallest = c;
System.out.println(smallest);
}
Method findSmallest
finds and prints the smallest of its 3 parameters a
, b
, and c
.
See Finding the minimum or maximum for an in depth discussion of the algorithm.
This version of findSmallest
prints the smallest value, which limits its usefulness. The keyword void
is used to specify that this version of findSmallest
does not return a value, so the method header includes: void findSmallest(...)
findSmallest
method with return value
public static int findSmallest(int a, int b, int c)
{
int smallest = a;
if(b < smallest)
smallest = b;
if(c < smallest)
smallest = c;
return smallest;
}
This version of findSmallest
uses the same algorithm to find the smallest value; however, it returns the value instead of printing it. Returning the smallest value makes this version of findSmallest
more flexible.
A method that returns a value gives back the value to the code that called the method.
The return type of a method is specified immediately before the method name. findSmallest
returns a value of type int
, so the method header includes: int findSmallest(...)
findLargest
method
public static int findLargest(int a, int b, int c)
{
int largest = a;
if(b > largest)
largest = b;
if(c > largest)
largest = c;
return largest;
}
Method findLargest
finds and returns the largest of its 3 parameters.
Calling a method with a return value
The value returned by a method call replaces the method call. A call to a method that returns a value can be used anywhere a value of that type could be used.
Example 1
System.out.println(findSmallest(5, 8, 3)); // prints: 3
The call to findSmallest
passes 5
, 8
, and 3
as the values of a
, b
, and c
, respectively. The call to findSmallest
returns 3
. The value 3
replaces the call to findSmallest
. When the println
method is called, the call is:
System.out.println(3);
Example 2
int d = 6, e = 4, f = 5;
int biggest = findLargest(d, e, f);
System.out.println(biggest); // prints: 6
The call to findLargest
passes 6
, 4
, and 5
as the values of a
, b
, and c
, respectively. The call to findLargest
returns 6
. The value 6
replaces the call to findLargest
. The assignment statement becomes:
...
int biggest = 6;
...
Example 3
System.out.println(findLargest(3, findSmallest(8, 7, 9), 6)); // prints 7
There are 3 method calls on this line: println
, findLargest
, and findSmallest
.
The call to findSmallest
executes first and returns 7
. The value returned by the call to findSmallest
replaces the method call. The line is now:
System.out.println(findLargest(3, 7, 6));
The call to findLargest
executes next and returns 7
. The value returned by the call to findLargest
replaces the method call. The line is now:
System.out.println(7);
The call to println
executes last and prints 7
.
Additional resources
See Return statements for demonstrations of additional Java rules.