What’s the difference between join()
and get()
of the CompletableFuture
class?
Both of them wait for the future completion.
The only difference is what kind of exceptions these two methods throw.
The get()
, originated in the Future
interface, throws “checked exceptions”.
While join()
throws “unchecked exceptions”.
In old days, like Java 1.5 era when Future.get()
was designed, checked exceptions might be preferred.
Nowadays, unchecked exceptions are more preferred, therefore join()
is preferred.
As per Javadoc of CompletableFuture,
To simplify usage in most contexts, this class also defines methods join() and getNow(T) that instead throw the CompletionException directly in these cases.
In other words, join()
is simpler to use, in the sense of exception handling.
It’s kind of an official recommendation.