What is Scala?

Scala is a modern multi-paradigm programming language designed to express common programming patterns in a concise, elegant, and type-safe way. It smoothly integrates features of object-oriented and functional languages.

Scala is object-oriented

Scala is a pure object-oriented language in the sense that every value is an object. Types and behaviour of objects are described by classes and traits(Traits are used to share interfaces and fields between classes. They are similar to Java 8’s interfaces. Classes and objects can extend traits but traits cannot be instantiated and therefore have no parameters.). Classes are extended by subclassing and a flexible mixin-based composition mechanism as a clean replacement for multiple inheritance.

Scala is functional

Scala is also a functional language in the sense that every function is a value. Scala provides a lightweight syntax for defining anonymous functions, it supports higher-order functions, it allows functions to be nested, and supports currying. Scala’s case classes and its built-in support for pattern matching model algebraic types used in many functional programming languages. Singleton objects provide a convenient way to group functions that aren’t members of a class.

Where to use Scala

  • Web applications
  • Utilities and libraries
  • Data streaming with Akka
  • Parallel batch processing
  • Concurrency and distributed application
  • Data analysis with Spark
  • AWS lambda expression
  • Ad hoc scripting in REPL etc.

In Scala, you can create any type of application in less time and coding whether it is web based, mobile based or desktop based application. Scala provides you powerful tools and API by using which you can create applications.

Setting up and getting started with Scala

IntelliJ is the most commonly-used IDE by Scala developers. In this tutorial, we’ll walk you through downloading and setting up IntelliJ with the Scala plugin, and we’ll get you started with your first Scala project.

  1. Make sure you have the Java 8 JDK (also known as 1.8)
  2. Run javac -version on the command line and make sure you see javac 1.8.___

    If you don’t have version 1.8 or higher, you can install from here install the JDK

  3. Next, download and install IntelliJ Community Edition
  4. Then, after starting up IntelliJ, you can download and install the Scala plugin by following the instructions: Press Ctrl+Alt+S or choose File | Settings (for Windows and Linux) or IntelliJ IDEA | Preferences (for macOS) on the main menu, and then go to Plugins and search for “Scala” in the plugins menu and installed it.

When we create the project, we’ll install the latest version of Scala.

Note: If you want to open an existing Scala project, you can click Open when you start IntelliJ.

Creating the Project
  1. Open up IntelliJ and click File => New => Project
  2. On the left panel, select Scala. On the right panel, select Scala once again.
  3. Name the project HelloWorld
Writing code
  1. On the Project pane on the left, right-click src and select New => Scala class.
  2. Name the class Hello and change the Kind to object.
  3. Change the code in the class to the following:
Run the Application

Right click on Hello in your code and select Run ‘Hello’.You’re done!!

Output :
“C:\Program Files\Java\jdk1.8.0_144\bin\java” “-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2017.3.4\lib\idea_rt.jar=53932:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2017.3.4\bin” -Dfile.encoding=UTF-8 -classpath “C:\Program Files\Java\jdk1.8.0_144\jre\lib\charsets.jar; Hello World!!

Trying Scala in the Browser

You can run Scala in your browser with ScalaFiddle.

  1. Go to https://scalafiddle.io.
  2. Paste println(“Hello, world!”) in the left pane.
  3. Hit “Run” button. Output appears in the right pane.

This is an easy, zero-setup way to experiment with pieces of Scala code.

This is it for Scala Introduction and HelloWorld example.In next article we will learn more about Scala. Keep Learning and Sharing! :)

Leave a Reply

Your email address will not be published. Required fields are marked *