Java The Complete Reference, Seventh Edition

(Greg DeLong) #1
System.out.println("Before the return.");

if(t) return; // return to caller

System.out.println("This won't execute.");
}
}

The output from this program is shown here:

Before the return.

As you can see, the finalprintln( )statement is not executed. As soon asreturnis executed,
control passes back to the caller.
One last point: In the preceding program, theif(t)statement is necessary. Without it, the
Java compiler would flag an “unreachable code” error because the compiler would know
that the lastprintln( )statement would never be executed. To prevent this error, theifstatement
is used here to trick the compiler for the sake of this demonstration.

104 Part I: The Java Language

Free download pdf