Sunday, 12 April 2015

Playing with Java 9 and REPL (LJC & Oracle Hackday)

Playing with Java 9 and REPL  (LJC & Oracle Hackday)

Java REPL  => Read-Evaluate-Print-Loop

JDK 9 - https://jdk9.java.net/download/ - b57
Kulla - https://java.net/projects/adoptopenjdk/downloads/download/kulla.jar


/ /Tells you which JDKs you have

% /usr/libexec/java_home -verbose

// You can set that export command in your ~/.profile(or .zshrc )
// and/or ~/.bashrc to permanently set JAVA_HOME
// for all shells/terminals/applications.

% export JAVA_HOME="`/usr/libexec/java_home -v '1.8*'`"

// Set JAVA_HOME to be Java 9 for the terminal
// window you are in

% export JAVA_HOME="`/usr/libexec/java_home -v '1.9*'`"

open up you .profile(mac users or .zshrc if you use zsh) and add 

export JAVA_HOME="`/usr/libexec/java_home -v '1.8*'`"

PS :: Here after when you open a new terminal jdk 1.8 is going to be your primary JDK.


% java -jar kulla.jar
|  Welcome to the Java REPL -- Version 0.401
|  Type /help for help

-> /help
Type a Java language expression, statement, or declaration.
Or type one of the following commands:

/l  or /list [all]                -- list the source you have typed
       /seteditor <executable>    -- set the external editor command to use
/e  or /edit <name or id>         -- edit a source entry referenced by name or id
/s  or /save [all|history] <file> -- save the source you have typed
/o  or /open <file>               -- open a file as source input
/v  or /vars                      -- list the declared variables and their values
/m  or /methods                   -- list the declared methods and their signatures
/c  or /classes                   -- list the declared classes
/x  or /exit                      -- exit the REPL
/r  or /reset                     -- reset everything in the REPL
/f  or /feedback <level>          -- feedback information: off, concise, normal, verbose, default, or ?
/p  or /prompt                    -- toggle display of a prompt
/cp or /classpath <path>          -- add a path to the classpath
/h  or /history                   -- history of what you have typed
       /setstart <file>           -- read file and set as the new start-up definitions
       /savestart <file>          -- save the default start-up definitions to the file
/?  or /help                      -- this help message

Supported shortcuts include:
<tab>       -- show possible completions for the current text
Shift-<tab> -- for current method or constructor invocation, show a synopsis of the method/constructor


 -> String myFoo = "Hello World";
|  Added variable myFoo of type String with initial value "Hello World"

-> /e 11
|  No id 11 found.  See /list

 -> /e add
|  No definition named add found.  See /classes /methods or /vars


-> /reset
|  Resetting state.


Resetting the repl editor

-> /exit
|  Goodbye


The earlier problem of was of using the version Version 0.401.

Download the latest version

https://java.net/projects/adoptopenjdk/downloads/download/kulla.jar.

% java -jar kulla.jar
|  Welcome to the Java REPL -- Version 0.411
|  Type /help for help


-> String foo = "Hello World";
|  Added variable foo of type String with initial value "Hello World"

-> int add(int a, int b) {
>> return a+b;
>> }


-> String add(String a, String b) {
>> return a+b;
>> }
|  Added method add


Method overloading in REPL

  10 : int add(int a, int b) {
           return a+b;
       }
  11 : String add(String a, String b) {
       return a+b;
       }
  12 : add(10,100);
  13 : add ("Hello", "world");




-> /vars
|    String foo = "Hello World"
|    int $1 = 110
|    String $2 = "Helloworld"

-> /v
|    String foo = "Hello World"
|    int $1 = 110
|    String $2 = "Helloworld"


-> /methods
|    printf (Ljava/lang/String;[Ljava/lang/Object;)V
|    add (Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;
|    add (II)I

-> /m
|    printf (Ljava/lang/String;[Ljava/lang/Object;)V
|    add (Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;
|    add (II)I


-> /list

   9 : String foo = "Hello World";
  10 : int add(int a, int b) {
           return a+b;
       }
  11 : String add(String a, String b) {
       return a+b;
       }
  12 : add(10,100);
  13 : add ("Hello", "world");

-> /list all

   1 : import java.util.*;
   2 : import java.io.*;
   3 : import java.math.*;
   4 : import java.net.*;
   5 : import java.util.concurrent.*;
   6 : import java.util.prefs.*;
   7 : import java.util.regex.*;
   8 : void printf(String format, Object... args) { System.out.printf(format, args); }
   9 : String foo = "Hello World";
  10 : int add(int a, int b) {
           return a+b;
       }
  11 : String add(String a, String b) {
       return a+b;
       }
  12 : add(10,100);
  13 : add ("Hello", "world");


-> /history

does print or lists out all the commands we have typed in till now.