return
ends the execution of the method in which it appears when it is called. For void methods, it simply exits the method body. For non-void methods, it actually returns a value (i.e. return X
). Just be careful with try-finally
: remember that the finally block will be executed even if you return
in the try
block:
public static void foo() {
try {
return;
} finally {
System.out.println("foo");
}
}
// run foo in main
foo
This is a good reference for learning more about return
.
is it like break
?
Well in the sense that both statements 'end' a running process; return
ends a method and break
ends a loop. Nevertheless, it is important to know the differences between the two and when each should be used.
if the second imageViewReused(photoToLoad)
returns true
, BitmapDisplayer bd=new BitmapDisplayer(bmp, photoToLoad)
won't be executed, right?
Correct - the method will "return
" if the body of that if
-statement is executed and no subsequent statements will be reached.