Home

Getting a Value from a Method in Java

|
|  Updated:  
2017-10-13 12:34:07
Java Essentials For Dummies
Explore Book
Buy On Amazon
Say that you’re sending a friend to buy groceries. You make requests for groceries in the form of method calls. You issue calls such as

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.

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.

A Java example

To see how return values and a return types work in a real Java program, check out the code below.

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;

importjava.text.NumberFormat;

classProcessGoodAccounts {

public static void main(String args[>) {

Random myRandom = new Random();

NumberFormat currency = NumberFormat.getCurrencyInstance();

<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> </strong>

<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.

running Java code Running the code.

How return types and return values work

Here’s what happens when getInterest is called:
  • The value of balance is 9508.00, and the value of rate is 2.0. So the value of balance * (rate / 100.0) is 190.16 — one hundred ninety dollars and sixteen cents.
  • The value 190.16 gets assigned to the interest variable, so the statement
return interest;

has the same effect as

return 190.16;
  • The return statement sends this value 190.16 back to the code that called the method. At that point in the process, the entire method call in the first set of code — anAccount.getInterest(interestRate) — takes on the value 190.16.
  • Finally, the value 190.16 gets assigned to the variable yearlyInterest.
java method call A method call is an expression with a value.

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.

Working with the method header

When you create a method or a method call, you have to be careful to use Java’s types consistently. Make sure that you check for the following:

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.

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!

About This Article

This article is from the book: 

About the book author: