concept return statement in category java

appears as: return statements, return statement, A return statement
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.

  • You can add labels to a code block defined using braces, {}, all looping statements (for, enhanced for, while, do-while), conditional constructs (if and switch statements), expressions and assignments, return statements, try blocks, and throws statements.
  • 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.

    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.

    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