Getting started with Scala
Tags: scala
Published: 2020-07-03 17:20:48

---
title: Getting started with Scala
published: true
date: 2016-10-17 22:00:00 UTC
tags: scala
cover_image: https://cdn.sanity.io/images/0a8atbln/production/49e1fc4af72a553a0578ebaddef09a97e9006d67-1920x1280.jpg?rect=0,100,1920,1080&w=1200&h=675&fit=crop&auto=format
canonical_url: https://devz.life/blog/getting-started-with-scala/
---

So you want to write your first Scala application? Before that, you need to install Scala on your PC and know some basic syntax.

First, you need to install [_Java JDK_](http://www.oracle.com/technetwork/java/javase/downloads/index-jsp-138363.html) and [_Scala binaries_](http://www.scala-lang.org/download/). To check if you have successfully installed Scala just write command “scala” in command prompt or terminal and if everything is OK you will see a welcome message.

 ![](https://cdn.sanity.io/images/0a8atbln/production/4e0761724cc5042df19ae703687684c393436d30-819x161.jpg)

Scala has _REPL_ (Read-Eval-Print-Loop) which allows you to write Scala code and immediately see results of your code in command prompt/terminal. _REPL_ allows you to experiment with all kind of Scala expressions and immediately see results.

 ![](https://cdn.sanity.io/images/0a8atbln/production/dbaaeacb001ee409f5ddff651278b9d763026fdf-818x430.jpg)

If you don’t like cmd/terminal you can use your favorite IDE or text editor. IntelliJ, Eclipse and NetBeans have good support for writing Scala code. Also if you want REPL in your IDE you have to create Scala worksheet and in them you can experiment with your code.

 ![](https://cdn.sanity.io/images/0a8atbln/production/b41878d2de3624ca31e987c445632da7f5a0a16c-639x136.jpg)
## 

## **Scala syntax**

**Variable declarations**

In Scala, you have two types of variables, immutable and mutable. Immutable variables are read-only and that means when you declare a variable, later you can’t assign new values/objects to that variable. An immutable variable is declared with keyword **_val_**.

```
val name: String = "Scala"

//shorter
val newName = "Scala"

// you can't assign new value
newName = "Java" // Error: reassignment to val name = "Java"
```

Mutable variables are declared with keyword **_var_** and later you can assign new values/objects to that variable.

```
var age = 23

//you can assign new values
age = 24
age = 25
```

So keywords **_val_** (immutable) and **_var_** (mutable) specify whether the reference can be changed to a different object (var) or not (val).

To declare variable you need to choose if you want **_immutable_** (val) or **_mutable_** (var) variable. Than you write name, after name you optionally write type of variable and on right side of equality you assign some value, object or expression. Defining type is optional because compiler can figure out it from right side of equality.

```
val nameOfImmutableVariable: type = value
var nameOfMutableVariable: type = value
```

**Method declarations**

Method declarations start with keyword **_def_**   **,** followed by optional argument list, optional return type and after equals sign body of the method.

```
def methodName (argument: argumentType): returnType = {
  //body of the method
}
```

Methods that don’t return some value for return type have a **_Unit_** type.

```
def printHelloWorld: Unit = {
  println("Hello world")
}
```

Also, you don’t have to write return keyword to return some value from the method, it’s enough to write value or some object that you want to return.

```
def sum(a: Int, b: Int): Int = {
  a + b
}
```

Scala methods can be written even more a concisely. Return type doesn’t have to be specified and parentheses around the body of the short method aren’t required.

```
def sum(a: Int, b: Int) = a + b
```

Scala is flexible when you working with methods. You can call object methods in two ways, the first way is to call a method on the object with dot character and the second way is like you write a sentence.

```
someobject.methodName()
//or
someobject methodName()
```

Also it’s important to mention when you call method which don’t take argument and has no side effects (pure functions) like mutating the state of objects or I/O operations than when you call method you don’t have to put rounded brackets after the method name.

```
List(1,2,3,4,5).size // 5
List(1,2,3,4,5).size() // error
```

**Classes**

To create Scala class you need to write keyword “class” and name of the class. A primary constructor of Scala class makes a combination of constructor parameters which are defined after class name and expressions and methods that are called in the body of the class. Depends on constructor parameters and fields types Scala automatically generates getter and setter methods. So if a field is mutable than are generated both getter and setter methods and if a field is an immutable than is generated the only getter method.

```
class Person(val firstName: String, val lastName: String){
  println("beginning of the constructor")
  var age = 25
  def printInfo = println("Firstname: " + firstName + ", lastname: " + lastName + ", age: " + age)
  printInfo
  println("end of the constructor")
}

val person = new Person("Matija","Kovacek")
/*
beginning of the constructor
Firstname: Matija, lastname: Kovacek, age: 25
end of the constructor
*/
// getter 
person.firstName
//setter
person.age = 30
```

**No semicolons**

You may have noticed that in previous examples I didn’t use semicolons. Scala is treating end of the line as the end of the statement or an expression so you don’t have to put semicolons. You only have to use semicolons when you want to put multiple statements or expressions on the same line.