It saves you from writing the boiler plate class file, compiling and executing it yourself (and is lighterweight than installing groovy/bsh).
Examples:
$ echo 'System.out.println("hello");' | ~/bin/javaexec
hello
$ echo 'String.format("%02d", 5)' | ~/bin/javaexec -p
05