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 WikipediaOpens in a new tab., 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 examplesOpens in a new tab.. 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-patternOpens in a new tab..

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 compilerOpens in a new tab.. 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 ScalaOpens in a new tab.. Then, we’ll probably want to get a copy of the solutionOpens in a new tab..

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.

Sample Programs in Every Language (44 Articles)—Series Navigation

For 100 Days of Code, I’ve decided to implement a few sample programs in as many languages as possible. Each implementation details a brief history of the language and a description of the code.

The plan for the series is to explore the major general-purpose language like Java, Python, C, C++, and C#. From there, we’ll take a look at some sample programs in web development languages like Ruby, PHP, and JavaScript. As we continue, we’ll cover proprietary languages like Swift and Objective-C. Eventually, we’ll start to tackle less popular languages like Rust, x86, and Verilog. Finally, we’ll play around with some of the esoteric languages like Brainf*ck and LOLCODE.

Who knows? Maybe the Sample Programs in Every Language series will become so popular it’ll never end. To help this series grow, consider sharing it on social media with your friends. Or, if you have a language you want to see, drop your suggestion in the comments.

Jeremy Grifski

Jeremy grew up in a small town where he enjoyed playing soccer and video games, practicing taekwondo, and trading Pokémon cards. Once out of the nest, he pursued a Bachelors in Computer Engineering with a minor in Game Design. After college, he spent about two years writing software for a major engineering company. Then, he earned a master's in Computer Science and Engineering. Today, he pursues a PhD in Engineering Education in order to ultimately land a teaching gig. In his spare time, Jeremy enjoys spending time with his wife, playing Overwatch and Phantasy Star Online 2, practicing trombone, watching Penguins hockey, and traveling the world.

Recent Posts