Hello World in Scala

Hello World in Scala Featured Image

Thanks for stopping by for this 18th installment of the Hello World in Every Language series. Today, we’re looking to implement Hello World in Scala, a Java clone from 2004.

Table of Contents

Scala Background

According to Wikipedia, Scala is another general-purpose programming language. Likewise, Scala is a multi-paradigm language. However, it does have functional capabilities, so I think it fits nicely into our recent string of functional languages: Lisp, Scheme, Racket, etc.

Now, Scala is a relatively new language. In fact, it was released in 2004, almost ten years after the release of Java. I mention Java because Scala was actually designed to correct many of Java’s problems. For instance, here’s a list of some of Scala’s features: a unified type system, syntactic flexibility, functional tendencies, and object-oriented extensions.

At this point, I should probably share an example to illustrate some of these differences, but I think Wikipedia already shares several excellent examples. In addition, I dug up a couple decent blogs comparing the two languages:

Since Scala has functional tendencies, I figured I’d dig into that a bit too. As it turns out, everything in Scala is an expression. And as a result, every expression is a function. This means that return statements are not required and are actually discouraged:

val myGrade = if (score >= 70) "Pass" else "Fail"

For those familiar with Java, this example probably looks odd. Fortunately, the solution is simple. Just evaluate the if-else expression and store the expected result in myGrade. If score is 90, then myGrade would store “Pass.” That’s pretty cool stuff!

At any rate, we probably shouldn’t dive too much further. After all, we have yet to implement Hello World in Scala.

Hello World in Scala

At long last, let’s implement Hello World in Scala:

object HelloWorld extends App {
  println("Hello, World!")
}

Up first, we have the class definition much like Java. However, there are two interesting keywords here: object and extends.

In Java, we would typically define a class using the class keyword. In fact, we normally even do that in Scala, so what’s with this object keyword? Well, as it turns out, object is used when we want to define a singleton.

In object-oriented languages, a singleton is an object which has a one and only one policy. In other words, only one instance of the object will ever exist. Personally, I’ve only ever used the singleton design pattern to track state in a video game. Beyond that, I would consider it an anti-pattern.

That said, singletons are a feature in Scala, and they’re typically used to define static functions. In other words, singletons can be used to generate utility classes that don’t need to be instantiated to access their functionality.

In addition, singletons in Scala are often used as companion objects, but I can’t say I totally understand what that is. Let me know in the comments.

Anyway, in this case, our singleton also extends App. This allows us to bypass the creation of a main method. We could have just as easily implemented Hello World in Scala as follows:

object HelloWorld {
    def main(args: Array[String]): Unit = {
        println("Hello, World!");
    }
}

At this point, we have something reminiscent of Java. Of course, the syntax is a bit different, but it looks about the same if we squint hard enough.

Finally, the only thing we have left is the print statement which is pretty typical at this point. Not much of a surprise there!

How to Run the Solution

If we want to try the code above, we can use an online Scala compiler. Just take the code above and drop it into the editor before hitting run.

Alternatively, we can always try to run the code locally. First, we’ll need to follow the directions when downloading and installing Scala. Then, we’ll probably want to get a copy of the solution.

With the heavy lifting out of the way, we should be able to simply run the following commands from the command line:

scalac hello-world.scala
scala hello-world

As we can see, Scala can be executed in pretty much the same way as Java. If all goes well, the last command should print the “Hello, World!” string.

Sample Programs in Every Language

And, there you have it – Hello World in Scala. Tomorrow, we’ll be looking at our last functional language, Elixir, before I start looking to change it up.

As always, if you have any languages you want to see, let me know in the comments. In addition, if you liked this article, give it a share. Every little bit helps.

Series Navigation← Hello World in PerlHello World in Elixir →
Advertisements

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.