Java 9 in practice REPL

Java 9 in practice: REPL

We are closely following the news of the Java 9 and already testing each of the many proposals ( JEPs ) which have been accepted and integrated into their most recent builds. The final release will occur only in March 2017, the phase in which all features should be complete next is : there are only two months ! This is the first post in a series that will allow you follow the whole evolution of the new version of the language to its long-awaited official release. The decision of which feature show that first post was not easy , there are many interesting news , but conveniently one of them will help us to test most of the others: REPL .

Read- Eval -Print – Loop ( REPL ) If you already program in languages like Scala , Ruby, and JavaScript Swift among many others, possibly already played with some kind of REPL . This tool is nothing more than a simple and interactive environment where you can easily execute code , providing a very effective way to try out new features and APIs. Worth reading this passage in this proposal motivation :

Exploration of coding options is also important for developers prototyping code or investigating a new API. Interactive evaluation is vastly more efficient in this regard than edit/compile/execute and System.out.println. Without the ceremony of class Foo { public static void main(String[] args) { … } }, learning and exploration is streamlined.

Installing the latest build of the JDK 9 First of all you need to install a recent build of the JDK 9 to test the contents of this and future posts . The process is not laborious , I recommend enough that you do not just in theory. In addition, new versions are always available , be sure to update whenever you experience a new feature. You find the latest build of the JDK 9 here , and after downloading , simply point your JAVA_HOME for this installation and use the java -version command to confirm that the process worked.

Download and run the REPL That done, you can already download the implementation of REPL Java 9, which is known as Kulla (aka JShell ) . There is always the option to clone the Mercurial repository design , build and buildar manually , but you do not need to do anything. To test , simply download the JAR available in this instance of the CloudBees AdoptOpenJDK . All ready , java -jar it and you will already be using the REPL . The build used in this post was kulla – 20160126010041.jar , if you want to use the same version .

Turini $ java -jar kulla–20160126010041.jar | Welcome to JShell — Version 9-ea| Type /help for help

A new way to experience Java code It’s time to put your hands on , but here’s a warning: it’s addictive. I do not go a day without using the REPL since he installed here for the first time , and I’m talking to experience day to day code, not only of the new language features . We can start by creating a new variable called value, which keep a whole :

int valor = 100;|  Added variable valor of type int with initial value 100

Note that there is a feedback for all your actions. You can disable it with the command / off feedback, but I personally like . Type / feedback and press the tab to see other options. Be sure to use the tab ( autocomplete ) whenever you use a new command to see all its possibilities .

With or without a semicolon ? Ah , you know the semicolon classic end of the line ? In REPL is optional :int value = 100| Modified variable value of type int with initial value 100 It is a convenient either , but I honestly have not got used .

Working with temporary variables And there’s more! You also do not need to declare the type of the variables :

semTipo = 100|  Error:|  cannot find symbol|    symbol:   variable semTipo|  semTipo = 100|  ^—–^

Oops , yes you need ! That was just one example for you to see the output of an invalid command. On the other hand, if you do not declare the variable name , the command will be accepted. Try just type the number 100 , or do some math operation without declaring the type and return variable :

valor / 3|  Expression value is: 33|    assigned to temporary variable $3 of type int

The result of the expression is saved in a temporary variable , in this case called $ 3 . See the feedback we warned the variable name and also its type. If you want to test , you can make a sysout the new variable :


Cool, huh? You can also use the command / vars to view all the available variables , including temporary :

/vars |    int valor = 100 |    int $3 = 33 |    double pi = 3.141592653589793

Deleting and editing variables You want to delete any of them? Use the / drop with the ID or name of the variable :/ Drop pi Or if you prefer , you can also edit it with the command / edit .

Ouch , that editor is that? If you are mine, you will prefer to do this in the world’s best editor : VIM . The command / seteditor help you with this :/ Seteditor came| Editor Set to : vimExploring the APIs Java Remember that you are in a Java environment, you can use any API class. Oh, and you can always use the TAB as a shortcut to autocomplete . In Math class , for example :


Try calling some of his methods :Math.random ( )| Expression value is : 0.12308153862256899| assigned to temporary variable $ 6 of type doubleDeclaring methods and classes The JSheel goes well beyond the simple variables and evaluações , you can also create methods or classes. Remember the classic challenge of fibonacci sequence ? How about to solve it like this:

long fibonacci(int n) {   if(n<2) return n;   return fibonacci(n-1) + fibonacci(n-2);}|  Added method fibonacci(int)

Let’s see if I hit the logic ? Can I is from 0 to 4 printing output:for (int i = 0; i < 5 ; i ++ )     System.out.println ( Fibonacci (i ) )011two3

We could also have created a Fibonacci class with this method inside but created out of a class just to show that it is accepted. You can use the commands / methods and / classes to list all available methods and classes. The / drop and / edit also work to delete and edit the classes and methods . What about imports ? And if the place is classic I wanted to use the Streams API Java 8 to iterate and print the Fibonacci sequence ? Do not miss it , just type Stream and ops …

Stream.of(1,2,3,4)|  Error:|  cannot find symbol|    symbol:   variable Stream|  Stream.of(1,2,3,4)|  ^—-^

I need to import this API ! So far we had not needed to import anything, since we only use classes and methods available in imports default . You can use the / imports to see what is available . To use the API Streams , just import it :

import . * Perfect, we can already use your classes will :Stream.of ( 1,2,3,4) .map (i -> fibonacci (i ) ) . ForEach ( System.out :: println )11two

Try also use the / help to see a list of all available commands.Following the news of the Java 9 We will always post news of the new version of the language here and Alura blog . The Java 9 mailing list is fairly busy , be sure to walk there . If you do not know Java 8 will like to see this course of Alura and also our Architecture course of Caelum . And you, what do you think of REPL ? I have the impression that is widely used in our day to day and also in the classroom . What do you think?


Please enter your comment!
Please enter your name here