Toilet Paper 109

The proof is in the pudding

Problem

The functionality of the Java SE library is very well described in the API documentation. Nevertheless, for some methods, it is still unclear how the return value looks like for very specific parameter values. The resulting uncertainty is often eliminated by simple trial and error. The required runtime environment is quickly provided with a static void main static void main (String[] args) method. Now insert the test code, compile and execute it, and evaluate the result. Depending on how fast you get the right result, you have to run through the 'change, compile, execute, evaluate' loop more or less often before you can delete the main method and the test code. Sounds complex? Indeed it is.

Solution

To remedy this, the JDK contains the JShell since the release of Java 9 in fall of 2017. With the help of this Read-Evaluate-Print Loop (short REPL), declarations, expressions and statements can be evaluated. The inputs are evaluated immediately and the results are displayed.

So if a JDK version 9 (or more current) is installed and JAVA_HOME is set, the JShell can be started:

$ jshell
|  Welcome to JShell -- Version 11.0.1
|  For an introduction type: /help intro

jshell>

You are not interested in a JShell because you use Java 8 in your project? Many classes and methods have remained unchanged over several Java versions. These classes or methods can be tested with a JShell without any problems.

Example

jshell> import java.text.SimpleDateFormat

jshell> new SimpleDateFormat("yyyy-mm-dd hh:MM").format(new Date())
$3 ==> "2019-39-30 06:01"            //--> whoops, something's wrong

jshell> new SimpleDateFormat("yyyy-MM-dd hh:mm").format(new Date())
$5 ==> "2019-01-30 06:39"            //--> ahhh, now it fits
JShell ToiletPaper

Wir verwenden Cookies, um unsere Webseite für Sie zu optimieren. Mit dem Besuch unserer Webseite erklären Sie sich damit einverstanden. // Our website is using cookies to improve your experience. By continuing to browse the site, you are agreeing to our use of cookies.

Weitere Informationen finden Sie in unserer Datenschutzerklärung. // For more information, please refer to our privacy policy.