UPDATE3: Java9 is now released with JShell!
UPDATE2: JShell to be introduced in Java9, so hopefully we will have a “native” Java REPL!
UPDATE1: Checkout a discussion about this post on Reddit!
One of the strong features of Python (and many other interpreted languages) is the availability of a REPL(Read, Evaluate, Print & Loop) interactive shell to execute code without having write a complete program and compile it to see the result. This substantially increases developer productivity especially when working with a large code base. Using a REPL shell (a.k.a. console) you can easily run and test small code snippets in isolation from your main program.
Now if Python can have a REPL why can’t Java? Well the answer is simple, Java is a “compiled” language. Undeterred, I still Googled for a “Java REPL”. Stackoverflow pointed me to Groovy Console. But before going onto Groovy Console, What is Groovy?
Groovy is an object-oriented programming language for the Java platform. It is a dynamic language with features similar to those of Python, Ruby, Perl, and Smalltalk. It can be used as a scripting language for the Java Platform, is dynamically compiled to Java Virtual Machine (JVM) bytecode, and interoperates with other Java code and libraries. Groovy uses a Java-like curly-bracket syntax. Most Java code is also syntactically valid Groovy.
The Groovy installation includes a REPL which is (obviously :)) called “Groovy Console”. Since “Most Java code is also syntactically valid Groovy” I can now use the Groovy Console to execute Java code snippets (see screenshot below).
This is great because now I don’t have to write a complete class and the dreaded “public static void main()” to test a small snippet of code before I merge it into my “big” program.
So there you go…if you want a “Java REPL” install Groovy!