Get startedGet started for free

Scala code and the Scala interpreter

1. Scala code and the Scala interpreter

So, Scala's exciting.

2. What is Scala?

We answered the "What" question quickly before. Let's dive deeper to find more answers to "Why use Scala?"

3. What is Scala?

Here's the first thing on Scala's official website. Pause the video and read through it. If that made your brain hurt, don't worry. Let's break it down.

4. Scala fuses OOP and FP

Scala fuses object-oriented and functional programming concepts more than any other language in the world. This fusion is what contributes the most to

5. Scala fuses OOP and FP -> Scala is scalable

the scalability of the language according Martin Odersky, creator of Scala.

6. Scala fuses OOP and FP -> Scala is scalable

Scala is object-oriented because every value is an object and every operation is a method call. You can write 2 plus 4 like this to get 6. Behind the scenes,

7. Scala fuses OOP and FP -> Scala is scalable

Scala rewrites it, with the method plus being called on the object 2. Rule of thumb to remember: pretty much everything is an object in Scala. This feature impacts scalability profoundly.

8. Scala fuses OOP and FP -> Scala is scalable

Scala is also a functional programming language, which is impossible to fully describe in one slide. For now, remember two things. First, in Scala, functions are first-class values, just like integers or strings. You can pass them as arguments to other functions, return them from functions, store them in variables, and more. This is not the case in all languages.

9. Scala fuses OOP and FP -> Scala is scalable

Second, operations of a program should map input values to output values rather than change data in place. Another way of saying this is functions should not have side effects. This may be a little confusing now and that's okay.

10. More answers to "Why use Scala?"

Beyond scalability, there are more answers to "Why use Scala?" Here's the shortlist. Don't worry if some of the next few concepts don't make full sense to you yet.

11. More answers to "Why use Scala?"

Scala is concise. Scala programs tend to be short, often down to one tenth of the number of lines compared to Java programs.

12. More answers to "Why use Scala?"

Scala is high-level, which means you won't deal with the details of the computer in your Scala code. In turn, your code becomes shorter and easier to understand. You have fewer opportunities to make mistakes.

13. More answers to "Why use Scala?"

Scala has an ADVANCED static type system that reduces verbosity in your code and adds language flexibility. These are two common criticisms of static typing.

14. More answers to "Why use Scala?"

Scala is compatible, which means you can build on previously existing Java code. That's huge. Scala runs primarily on the Java Virtual Machine. It heavily reuses Java types. Plus more.

15. The Scala interpreter

Now, let's write some Scala code! The Scala interpreter is a perfect place to start. First you need a working Scala installation, which is a step taken care of for you on DataCamp. You start the Scala interpreter by typing scala at a command prompt. Scala then prompts YOU to write Scala code, signified by the word scala followed by the greater than symbol.

16. The Scala interpreter

2 plus 3. The interpreter then reads the code you wrote, wraps it in an executable template, and then compiles and executes the result for you. The interpreter prints res0 colon Int equals five. res0 is short for result zero, an autogenerated name that refers to the result. Colon Int is called a type annotation where Int means integer type. More on types shortly. Finally, there is equals sign and the value of the expression (five). res0 can be reused. Multiplying it by two, the interpreter prints res1, an Int, equal to ten.

17. The Scala interpreter

The print line function with a string of characters in quotes prints to standard output. Playing Twenty-One sounds fun!

18. Let's practice!

Get practicing so you can write code that does that.