Scala Basics

Expressions are computable statements.

1 + 1

You can output results of expressions using println.

println(1) // 1
println(1 + 1) // 2
println(“Hello!”) // Hello!
println(“Hello,” + ” world!”) // Hello, world!

You can name results of expressions with the val keyword.

val x = 1 + 1
println(x) // 2

Named results, such as x here, are called values. Referencing a value does not re-compute it.

Values cannot be re-assigned.

x = 3 // This does not compile.

Types of values can be inferred, but you can also explicitly state the type, like this:

val x: Int = 1 + 1

Notice how the type declaration Int comes after the identifier x. You also need a :.

Variables are like values, except you can re-assign them. You can define a variable with the var keyword.

var x = 1 + 1
x = 3 // This compiles because “x” is declared with the “var” keyword.
println(x * x) // 9

As with values, you can explicitly state the type if you want:

var x: Int = 1 + 1

You can combine expressions by surrounding them with {}. We call this a block.

The result of the last expression in the block is the result of the overall block, too.

val x = 1 + 1
x + 1
}) // 3


Functions are expressions that take parameters.

You can define an anonymous function (i.e. no name) that returns a given integer plus one:

(x: Int) => x + 1

On the left of => is a list of parameters. On the right is an expression involving the parameters.

You can also name functions.

val addOne = (x: Int) => x + 1
println(addOne(1)) // 2

Functions may take multiple parameters.

val add = (x: Int, y: Int) => x + y
println(add(1, 2)) // 3

Or it can take no parameters.

val getTheAnswer = () => 42
println(getTheAnswer()) // 42

Methods look and behave very similar to functions, but there are a few key differences between them.

Methods are defined with the def keyword. def is followed by a name, parameter lists, a return type, and a body.

def add(x: Int, y: Int): Int = x + y
println(add(1, 2)) // 3

Notice how the return type is declared after the parameter list and a colon : Int.

Methods can take multiple parameter lists.

def addThenMultiply(x: Int, y: Int)(multiplier: Int): Int = (x + y) * multiplier
println(addThenMultiply(1, 2)(3)) // 9

Or no parameter lists at all.

def name: String = System.getProperty(“”)
println(“Hello, ” + name + “!”)

There are some other differences, but for now, you can think of them as something similar to functions.

Methods can have multi-line expressions as well.

def getSquareString(input: Double): String = {
val square = input * input

The last expression in the body is the method’s return value. (Scala does have a return keyword, but it’s rarely used.)


You can define classes with the class keyword followed by its name and constructor parameters.

class Greeter(prefix: String, suffix: String) {
def greet(name: String): Unit =
println(prefix + name + suffix)

The return type of the method greet is Unit, which says there’s nothing meaningful to return. It’s used similarly to void in Java and C. (A difference is that because every Scala expression must have some value, there is actually a singleton value of type Unit, written (). It carries no information.)

You can make an instance of a class with the new keyword.

val greeter = new Greeter(“Hello, “, “!”)
greeter.greet(“Scala developer”) // Hello, Scala developer!

We will cover classes in depth later.

Case Classes
Scala has a special type of class called a “case” class. By default, case classes are immutable and compared by value. You can define case classes with the case class keywords.

case class Point(x: Int, y: Int)

You can instantiate case classes without new keyword.

val point = Point(1, 2)
val anotherPoint = Point(1, 2)
val yetAnotherPoint = Point(2, 2)

And they are compared by value.

if (point == anotherPoint) {
println(point + ” and ” + anotherPoint + ” are the same.”)
} else {
println(point + ” and ” + anotherPoint + ” are different.”)
} // Point(1,2) and Point(1,2) are the same.

if (point == yetAnotherPoint) {
println(point + ” and ” + yetAnotherPoint + ” are the same.”)
} else {
println(point + ” and ” + yetAnotherPoint + ” are different.”)
} // Point(1,2) and Point(2,2) are different.

There is a lot more to case classes that we’d like to introduce, and we are convinced you will fall in love with them! We will cover them in depth later.


Objects are single instances of their own definitions. You can think of them as singletons of their own classes.

You can define objects with the object keyword.

object IdFactory {
private var counter = 0
def create(): Int = {
counter += 1

You can access an object by referring to its name.

val newId: Int = IdFactory.create()
println(newId) // 1
val newerId: Int = IdFactory.create()
println(newerId) // 2

We will cover objects in depth later.


Traits are types containing certain fields and methods. Multiple traits can be combined.

You can define traits with trait keyword.

trait Greeter {
def greet(name: String): Unit

Traits can also have default implementations.

trait Greeter {
def greet(name: String): Unit =
println(“Hello, ” + name + “!”)

You can extend traits with the extends keyword and override an implementation with the override keyword.

class DefaultGreeter extends Greeter

class CustomizableGreeter(prefix: String, postfix: String) extends Greeter {
override def greet(name: String): Unit = {
println(prefix + name + postfix)

val greeter = new DefaultGreeter()
greeter.greet(“Scala developer”) // Hello, Scala developer!

val customGreeter = new CustomizableGreeter(“How are you, “, “?”)
customGreeter.greet(“Scala developer”) // How are you, Scala developer?

Here, DefaultGreeter extends only a single trait, but it could extend multiple traits.

We will cover traits in depth later.

Main Method

The main method is an entry point of a program. The Java Virtual Machine requires a main method to be named main and take one argument, an array of strings.

Using an object, you can define a main method as follows:

object Main {
def main(args: Array[String]): Unit =
println(“Hello, Scala developer!”)

Have any Question or Comment?

8 comments on “Scala Basics

Hello! I simply would like to give an enormous thumbs up for the nice info you’ve got here on this post. I can be coming back to your weblog for extra soon.


This actually answered my problem, thanks!


I am regular reader, how are you everybody? This article posted at this web site is truly


Hi there, You’ve done an incredible job. I will certainly
digg it and personally recommend to my friends.
I’m confident they will be benefited from this website.


I love it when people get together and share thoughts.
Great site, keep it up!


Hmm it appears like your website ate my first comment (it was super long) so I guess I’ll just
sum it up what I submitted and say, I’m thoroughly enjoying your
blog. I too am an aspiring blog writer but I’m still new
to everything. Do you have any recommendations for rookie blog writers?
I’d certainly appreciate it.


Fantastic goods from you, man. I have understand your stuff previous to and you are just extremely wonderful.
I actually like what you’ve acquired here, certainly like what you’re saying and the way in which you say
it. You make it enjoyable and you still take care of to keep it wise.

I cant wait to read much more from you. This is actually a tremendous site.


For most recent information you have to pay a visit internet and on world-wide-web
I found this site as a most excellent web page for newest updates.


Leave a Reply

Your email address will not be published.