goToTheSupermarketAndBuySome(bread);
goToTheSupermarketAndBuySome(bananas);
The things in parentheses are parameters. Each time you call your goToTheSupermarketAndBuySome method, you put a different value in the method’s parameter list.
goToTheSupermarketAndBuySome
Now what happens when your friend returns from the supermarket? “Here’s the bread you asked me to buy,” says your friend. As a result of carrying out your wishes, your friend returns something to you. You made a method call, and the method returns information (or better yet, the method returns some food).
The thing returned to you is called the method’s return value, and the type of thing returned to you is called the method’s return type.
This code shows a method that returns a value
importjava.text.NumberFormat;
import static java.lang.System.out;
classGoodAccount {
String lastName;
int id;
double balance;
<strong> </strong>
<strong>double getInterest(double rate) {</strong>
<strong>double interest;</strong>
out.print("Adding ");
out.print(rate);
out.println(" percent…");
<strong>interest = balance * (rate / 100.0);</strong>
<strong>return interest;</strong>
<strong>}</strong>
void display() {
NumberFormat currency = NumberFormat.getCurrencyInstance();
out.print("The account with last name ");
out.print(lastName);
out.print(" and ID number ");
out.print(id);
out.print(" has balance ");
out.println(currency.format(balance));
}
This code calls the method in the code above.
importjava.util.Random;
classProcessGoodAccounts {
public static void main(String args[>) {
Random myRandom = new Random();
<strong>GoodAccount</strong>anAccount;
doubleinterestRate;
<strong>doubleyearlyInterest;</strong>
for (int i = 0; i < 3; i++) {
anAccount = new GoodAccount();
anAccount.lastName = "" +
(char) (myRandom.nextInt(26) + 'A') +
(char) (myRandom.nextInt(26) + 'a') +
(char) (myRandom.nextInt(26) + 'a');
anAccount.id = myRandom.nextInt(10000);
anAccount.balance = myRandom.nextInt(10000);
anAccount.display();
<strong>interestRate = myRandom.nextInt(5);</strong>
<strong>yearlyInterest = anAccount.getInterest(interestRate);</strong>
System.out.print("This year's interest is ");
System.out.println(currency.format(yearlyInterest));
System.out.println();
Here’s a run from the code.
getInterest
balance
9508.00
rat
2.0
balance * (rate / 100.0)
190.16
interest
return interest;
has the same effect as
return 190.16;
return
— anAccount.getInterest(interestRate) —
yearlyInterest
If a method returns anything, a call to the method is an expression with a value. That value can be printed, assigned to a variable, added to something else, or whatever. Anything you can do with any other kind of value, you can do with a method call.
In the first set of code, the getInterest method’s header starts with the word double. When the method is executed, it should send a double value back to the place that called it.
double
Again in the first set of code, the last statement in the getInterest method is return interest. The method returns whatever value is stored in the interest variable, and the interest variable has type double. So far, so good.
In the second set of code, the value returned by the call to getInterest is assigned to a variable named yearlyInterest. Sure enough, yearlyInterest is of type double.
That settles it! The use of types in the handling of method getInterest is consistent in both sets of code. I’m thrilled!
Dr. Barry Burd holds an M.S. in Computer Science from Rutgers University and a Ph.D. in Mathematics from the University of Illinois. Barry is also the author of Beginning Programming with Java For Dummies, Java for Android For Dummies, and Flutter For Dummies.