Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 1a713796b4
Fetching contributors…

Cannot retrieve contributors at this time

98 lines (80 sloc) 4.993 kb
layout
default

First Steps: Compiling and Running

Before we can start programming in Scala, we need to install Scala, that is, the compiler and the standard library. We won't be using the features of any specific IDE in this tutorial, Scala is supported by all major IDE platforms, so pick your favorite one. You can also write the code in a text editor and compile and run your programs from a shell (command line). Whatever you do, you also need to have a JDK installed.

If you have no strong preference for any specific IDE or editor, I'd recommend the Scala IDE for Eclipse. It already comes with the Scala compiler and standard library, so you don't need to install them separately. All you need is Eclipse Helios or later and the Scala IDE plug-in. Here are some alternatives if you prefer to work with a different IDE:

Now it's time for your first Scala program so you can check if your setup was correct. Create a new file ending in ".scala" with the following content:

{% highlight scala %} package chapter1

object HelloWorld { def main(args: Array[String]): Unit = { println("Hello, " + args(0)) } } {% endhighlight %}

Let's go through this code. The first line specifies that we create a package named chapter1 to contain our first example. Packages describe a hierarchical organization of the code and are used to structure your code at large.

If you're coming from Java, you're already familiar with packages, but the next declaration might be surprising: an object named HelloWorld. An object is almost like a class, except that the compiler automatically creates one single instance -- that's why their full name is singleton objects, like the infamous design pattern -- for you and that you cannot create new instances of it. Objects supersede Java and C++'s static methods and variables: you can think of all the methods and variables inside an object as being static. Objects can also extend other classes, implement interfaces and be used just like any other intance of a class.

The entry point for every Scala program is the main method, which is our only declaration in the HelloWorld object. Method declarations in Scala start with the def keyword, followed by the name of the method. Next comes the list of parameters, which is an Array of Strings -- the arguments the user gives when running our program. Scala makes the names of variables and methods more prominent by putting the type after the name. Analog, the result type of the method follows after the list of parameters. An equality sign separates the signature and the body of the method.

Our main-method simply prints the first argument of the args array to the command line. The println method looks like a free function, but it's actually defined in the Predef object, whose members are all automatically imported by Scala. The result type of println is Unit, which matches the result type of the main method. The Unit type has exactly one value, denoted by () and is used for methods that don't produce a result. Unit is similar to the void type in other languages (you can also think of () as a tuple with zero components).

Scala can actually infer the result type of methods with a single exit point, so it would be valid to omit the : Unit in the code. We use the println function to retrieve the first argument and concatenate it with the string "Hello, ".

Don't let the different kinds of brackets confuse you: Instead of Java and C++'s <> for type or template parameters, Scala uses []. Indexing an array does not have get any special syntax (actually, Scala does not treat arrays special at all), as we have seen in the example, so there's no index-operator. The rules are quite simple: value parameters use () and type parameters []. Statement lists are enclosed in {}, this one's the same in all 3 languages.

Exercises:

  1. Change the program so it reads two arguments from the command line, and prints out a message using both of them.
  2. Change the program so it performs a simple arithmetic operation on the two arguments and prints out the result. You can use the toInt method to convert a string to a number, and toString to convert a number back into a string. Play around with different operations.
  3. getLine is a method that reads a line from the console and returns it as a string. Change the program so it prompts for a name, reads the name, and then prints that instead of the command line value.
Jump to Line
Something went wrong with that request. Please try again.