Scala Book

Categories:

Recommended

Chapter – 1 Introduction

In these pages, Scala Book provides a quick introduction and overview of the Scala programming language. The book is written in an informal style, and consists of more than 50 small lessons. Each lesson is long enough to give you an idea of how the language features in that lesson work, but short enough that you can read it in fifteen minutes or less.

One note before beginning:

  • In regards to programming style, most Scala programmers indent their code with two spaces, but we use four spaces because we think it makes the code easier to read, especially in a book format.

To begin reading, click the “next” link, or select the Prelude: A Taste of Scala lesson in the table of contents.

Chapter – 2  Prelude: A Taste of Scala

Our hope in this book is to demonstrate that Scala is a beautiful, modern, expressive programming language. To help demonstrate that, in this first chapter we’ll jump right in and provide a whirlwind tour of Scala’s main features. After this tour, the book begins with a more traditional “Getting Started” chapter.

In this book we assume that you’ve used a language like Java before, and are ready to see a series of Scala examples to get a feel for what the language looks like. Although it’s not 100% necessary, it will also help if you’ve already downloaded and installed Scala so you can test the examples as you go along. You can also test these examples online with ScalaFiddle.io.

2.1 Overview

Before we jump into the examples, here are a few important things to know about Scala:

2.2 Hello, world

Ever since the book, C Programming Language, it’s been a tradition to begin programming books with a “Hello, world” example, and not to disappoint, this is one way to write that example in Scala:
object Hello extends App {
println(“Hello, world”)
}

After you save that code to a file named Hello.scala, you can compile it with scalac:
$ scalac Hello.scala

If you’re coming to Scala from Java, scalac is just like javac, and that command creates two files:

  • Hello$.class
  • Hello.class

These are the same “.class” bytecode files you create with javac, and they’re ready to run in the JVM. You run the Hello application with the scala command:
$ scala Hello

We share more “Hello, world” examples in the lessons that follow, so we’ll leave that introduction as-is for now.

2.3 The Scala REPL

The Scala REPL (“Read-Evaluate-Print-Loop”) is a command-line interpreter that you use as a “playground” area to test your Scala code. We introduce it early here so you can use it with the code examples that follow.

To start a REPL session, just type scala at your operating system command line, and you’ll see something like this:

$ scala
Welcome to Scala 2.13.0 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_131). Type in expressions for evaluation. Or try :help.

scala> _

Because the REPL is a command-line interpreter, it just sits there waiting for you to type something. Inside the REPL you type Scala expressions to see how they work:

scala> val x = 1
x: Int = 1

scala> val y = x + 1
y: Int = 2

As those examples show, after you type your expressions in the REPL, it shows the result of each expression on the line following the prompt.

2.4 Two types of variables

Scala has two types of variables:

  • val is an immutable variable — like final in Java — and should be preferred
  • var creates a mutable variable, and should only be used when there is a specific reason to use it
  • Examples:

val x = 1 //immutable
var y = 0 //mutable

2.5 Declaring variable types

In Scala, you typically create variables without declaring their type:

val x = 1
val s = “a string”
val p = new Person(“Regina”)

When you do this, Scala can usually infer the data type for you, as shown in these REPL examples:

scala> val x = 1
val x: Int = 1

scala> val s = “a string”
val s: String = a string

This feature is known as type inference, and it’s a great way to help keep your code concise. You can also explicitly declare a variable’s type, but that’s not usually necessary:

val x: Int = 1
val s: String = “a string”
val p: Person = new Person(“Regina”)

As you can see, that code looks unnecessarily verbose.

Category:

Attribution

Alvin Alexander (2020),Scala Book, URL: https://alvinalexander.com/scala/scala-book-free/

This work is licensed under the Attribution-NonCommercial-ShareAlike 3.0 Unported:
(https://creativecommons.org/licenses/by-nc-sa/3.0/legalcode
).

VP Flipbook Maker

Convert your work in different formats to digital flipbook and share it with others! Visual Paradigm Online is a professional tool for you to do flipbook conversion and creation. Try it now!