Scala HelloWorld App in the Scala REPL

We will see how to create Scala HelloWorld App in Scala REPL as follows:

$ scala

Welcome to Scala 2.11.8 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_77). Type in expressions for evaluation. Or try :help. scala>object HelloWorld extends App {

| println("Hello World")

| }

defined object HelloWorld scala>HelloWorld object HelloWorld


Hello World scala>

After coding the HelloWorld object in the Scala REPL we can ask the REPL what HelloWorld is, and as you might realize, the REPL will answer that HelloWorld is an object. This is a very convenient Scala way to code console applications, because we can have a Hello World application with just three lines of code. Sadly, to have the same program in Java, it required way more code. Java is a great language for performance, but it is a verbose language compared with Scala, for instance.

Java HelloWorld application

We will see how to create Java HelloWorld application as follows:

package scalabook.javacode.chapl;

public class HelloWorld {

public static void main(String args[]){

System.out.println("Hellow World");



The Java app required six lines of code, while in Scala, we were able to do the same with 50% less code (three lines of code). This is a very simple application. When we are coding complex applications, this difference gets bigger, as a Scala application ends up with way less code than Java.

Remember, we use an object in Scala in order to have a Singleton (Design Pattern that makes sure you have just one instance of a class), and if we want the same in Java, the code would be something like the following: package scalabook.javacode.chapl;

public class HelloWorldSingleton {

private HelloWorldSingleton(){}

private static class SingletonHelper{

private static final HelloWorldSingleton INSTANCE = new HelloWorldSingleton();


public static HelloWorldSingleton getInstance(){ return SingletonHelper.INSTANCE;


public void sayHello(){

System.out.println("Hello World");


public static void main(String[] args) { getInstance().sayHello();



It's not just about the size of the code, but also about consistency and the language providing more abstractions for you. If you write less code, you will have fewer bugs in your software at the end of the day.

< Prev   CONTENTS   Source   Next >