I recently was part of a small java programming competition at my school. My partner and I have just finished our first pure oop class and most of the questions were out of
Even faster than all of that is using the Miller-Rabin test. It's a probabilistic test, and therefore has a certain level of error; however, the test runs multiple times which shrinks that error as small as needed (50 often suffices for commercial applications).
Not in Java, but here's a quick implementation in Python I cooked up.