dummies logo
Articles

Article Categories

close
TechnologyAcademics & The ArtsHome, Auto, & HobbiesBody, Mind, & SpiritBusiness, Careers, & Money
Collections

Collections

Explore all collections
close
BYOB (Be Your Own Boss)Be a Rad DadCareer ShiftingContemplating the CosmosFor Those Seeking Peace of MindFor the Aspiring AficionadoFor the Budding Cannabis EnthusiastFor the College BoundFor the Exam-Season CrammerFor the Game Day Prepper
Custom Solutions
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
forward arrow
  • Custom Solutions
  • Main Menu
  • Collections

    Explore all collections
    • BYOB (Be Your Own Boss)
    • Be a Rad Dad
    • Career Shifting
    • Contemplating the Cosmos
    • For Those Seeking Peace of Mind
    • For the Aspiring Aficionado
    • For the Budding Cannabis Enthusiast
    • For the College Bound
    • For the Exam-Season Crammer
    • For the Game Day Prepper
    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 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.
    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: 

    Beginning Programming with Java For Dummies

    About the book author:

    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.

    This article can be found in the category: 

    Java
    Trending
    Format Numbers in Thousands and Millions in Excel Reports
    Doodle Dogs For Dummies Cheat Sheet
    How to Play Rummy: All You Need to Know
    From Category
    Java
    Important Features of the Java Language
    How to Download and Install TextPad
    How to Install JavaFX and Scene Builder
    A Few Things about Java GUIs
    Let the Objects Do the Work in Java

    Quick Links

    About For Dummies
     Terms of Use
    Copyright © 2000-2025 by John Wiley & Sons, Inc., or related companies. All rights reserved, including rights for text and data mining and training of artificial technologies or similar technologies.
    © 2025 MARVEL

    Follow Lee on X/Twitter - Father, Husband, Serial builder creating AI, crypto, games & web tools. We are friends :) AI Will Come To Life!

    Check out: eBank.nz (Art Generator) | Netwrck.com (AI Tools) | Text-Generator.io (AI API) | BitBank.nz (Crypto AI) | ReadingTime (Kids Reading) | RewordGame | BigMultiplayerChess | WebFiddle | How.nz | Helix AI Assistant