scala-vs-java

Scripting

Java

Java 9

Java 9 is the first version of Java with a REPL. It’s called jshell.

Just run it with the jshell command.

    $ jshell

    |  Welcome to JShell -- Version 9
    |  For an introduction type: /help intro

    jshell> String[] fruits = {"apple", "banana", "orange"}
    fruits ==> String[3] { "apple", "banana", "orange" }

    jshell> Arrays.asList(fruits).forEach(System.out::println)
    apple
    banana
    orange

Java 8 and before

Before Java 9, Java had no REPL. You could/still can use third party or open source REPLS such as JavaREPL which also has a browser-based REPL.

Scala

Scala has a REPL which you can run narutally with the scala command.

    $ scala

    Welcome to Scala 2.12.3 (Java HotSpot(TM) 64-Bit Server VM, Java 9).
    Type in expressions for evaluation. Or try :help.

    scala> val fruits = Array("apple", "banana", "orange")
    fruits: Array[String] = Array(apple, banana, orange)

    scala> fruits.foreach(println)
    apple
    banana
    orange

If you want to paste multiple lines just type :paste. Type ctrl-D at the end to terminate the paste mode.

Ammonite

Ammonite Shell and REPL are very powerfool tools. Check them out if you need a full fledged shell/REPL.