concept return in category java

appears as: return, returns
Java SE 11 Programmer I Certification Guide MEAP V03

This is an excerpt from Manning's book Java SE 11 Programmer I Certification Guide MEAP V03.

boolean isPrime(int num) {                  #A
    boolean result = true;                        #B
    if (num <= 1) return false;
    for (int ctr = num-1; ctr > 1; ctr--) {       #C
        if (num%ctr == 0) result = false;
    }
    return result;
}
  • return statement
  • Figure 8.2 shows the code of a method accepting method parameters and defining a return type and a return statement.

    Figure 8.2 An example of a method that accepts method parameters and defines a return type and a return statement

    A return statement is used to exit from a method, with or without a value. For methods that define a return type, the return statement must be immediately followed by a return value. For methods that don’t return a value, the return statement can be used without a return value to exit a method. Figure 8.4 illustrates the use of a return statement.

    Figure 8.4 An example of a method that accepts method parameters and defines a return type and a return statement

    In this example, method calcAverage returns a value of type double, using a return statement:

    double calcAverage(int marks1, int marks2) {
        double avg = 0;
        avg = (marks1 + marks2)/2.0;
        return avg;                     #A
    }

    The methods that don’t return a value (the return type is void) aren’t required to define a return statement:

    void setWeight(double val) {      #A
        weight = val;                 #A
    }                                 #A

    But you can use the return statement in a method even if it doesn’t return a value. Usually this statement is used to define an early exit from a method:

    void setWeight(double val) {     #A
        if (val < 0.7) return;        #B
        weight = val;
    }

    Also, the return statement must be the last statement to execute in a method, if present. The return statement transfers control out of the method, which means that there’s no point in defining any code after it. The compiler will fail to compile such code:

    void setWeight(double val) {
        return;        #A
        weight = val;  #B
    }

    Note that there’s a difference in the return statement being the last statement in a method and being the last statement to execute in a method. The return statement need not be the last statement in a method, but it must be the last statement to execute in a method:

    void setWeight(double val) {
        if (val < 0)
            return;
        else
            weight = val;
    }

    In the preceding example, the return statement isn’t the last statement in this method. But it’s the last statement to execute for method parameter values of less than zero.

    OCA Java SE 8 Programmer I Certification Guide

    This is an excerpt from Manning's book OCA Java SE 8 Programmer I Certification Guide.

    Figure 3.14. An example of a method that accepts method parameters and defines a return type and a return statement
    public void addNumbers(byte arg1, int arg2, int arg3) {
        double sum = arg1 + arg2 + arg3;
    }

    A return statement is used to exit from a method, with or without a value. For methods that define a return type, the return statement must be immediately followed by a return value. For methods that don’t return a value, the return statement can be used without a return value to exit a method. Figure 3.16 illustrates the use of a return statement.

    Figure 3.16. An example of a method that accepts method parameters and defines a return type and a return statement

    In this example, we’ll revisit the previous example of method calcAverage, which returns a value of type double, using a return statement:

    The methods that don’t return a value (the return type is void) aren’t required to define a return statement:

    But you can use the return statement in a method even if it doesn’t return a value. Usually this statement is used to define an early exit from a method:

    Also, the return statement must be the last statement to execute in a method, if present. The return statement transfers control out of the method, which means that there’s no point in defining any code after it. The compiler will fail to compile such code:

    Note that there’s a difference in the return statement being the last statement in a method and being the last statement to execute in a method. The return statement need not be the last statement in a method, but it must be the last statement to execute in a method:

    void setWeight(double val) {
        if (val < 0)
            return;
        else
            weight = val;
    }

    In the preceding example, the return statement isn’t the last statement in this method. But it’s the last statement to execute for method parameter values of less than zero.

  • Keyword return (optional)
  • The Well-Grounded Java Developer

    This is an excerpt from Manning's book The Well-Grounded Java Developer.

    In order to use the agent finders, the project has a default HollywoodService class that gets a list of agents from a SpreadsheetAgentFinder, filters them on friendliness, and returns that friendly list as shown in the following code listing.

  • return statements
  • Another optional piece of syntax is that you don’t need to specify the return keyword when returning an object or value from a method. Groovy will automatically return the result of the last evaluated expression.

    Listing 8.1. Semicolons and returns are optional
    sitemap

    Unable to load book!

    The book could not be loaded.

    (try again in a couple of minutes)

    manning.com homepage
    test yourself with a liveTest