Skip to main content
IBM 
ShopSupportDownloads
IBM HomeProductsConsultingIndustriesNewsAbout IBM
Java Language EssentialsDownload tutorial zip fileEmail this tutorial to a friend
Main menuSection menuGive feedback on this tutorialPrevious panelNext panel
Course Notes
  


Methods that return values page 15 of 37


With these new variables, we can provide convenience methods such as


  boolean isGoodWithChildren() {
    if (gentle == true && obedienceTrained == true)
      return true;
    else
      return false;
  }

This method introduces additional Java(TM) syntax. First, instead of the modifier void, isGoodWithChildren() provides a return type, in this case, boolean. With the replacement of void by either a primitive, system-, or user-defined data type, the method must provide a return statement for every execution path possible through the method--the Java compiler enforces this "contract."

A return statement has the syntax


return <value>;

where <value> is any expression that evaluates to the appropriate return data type.

For isGoodWithChildren(), if the if statement's Boolean expression evaluates to true, the first return executes, which terminates the method execution and returns the result of the evaluation, which in this case is simply the literal value true. If the Boolean expression evaluates to false, the code block following the else clause is evaluated, in this case, a single return statement that returns false.

In this example, the Boolean expression is compound--it includes two expressions, each with the == comparison operator. The comparative expressions are linked with the logical and operator &&; hence, the complete expression evaluates to true only if both subexpressions evaluate to true.

Boolean expressions often include one or more Java logical operators, which are listed in the following table:

Logical OperatorInterpretation
&&and ("short-circuit" version)
&and ("full-evaluation" version)
||or ("short-circuit" version)
|or ("full-evaluation" version)

With the "short-circuit" versions, evaluation of subsequent subexpressions is abandoned as soon as a subexpression evaluates to false (in the case of &&) or true (in the case of ||).

Although isGoodWithChildren() provides a full illustration of if, including the optional else clause, this method can be coded more succinctly. Java, like C, is a syntactically powerful language. First, we can actually remove the if because the return values correspond to the Boolean expression, that is, if the Boolean expression evaluates to true, return true; otherwise, return false. The more concise implementation is:


  boolean isGoodWithChildren() {
    return (gentle == true &amp;&amp; obedienceTrained == true);
  }

One more reduction is possible. Note that each subexpression involves a boolean variable compared to the boolean literal true. In this case, each subexpression can be reduced to the boolean variable itself:


  boolean isGoodWithChildren() {
    return (gentle && obedienceTrained);
  }

copyright 1996-2000 Magelang Institute dba jGuru


Main menuSection menuGive feedback on this tutorialPrevious panelNext panel
PrivacyLegalContact