Introduction to Scala Programming Language

Filed Under: Scala

Scala is a programming language that integrates the object oriented and functional language features. This programming language was developed by Martin Odersky and released in 2003. It has been used to develop several scalable concurrent applications in the recent years.

Scala Features

The features of Scala are as listed below;

  • Object Oriented: Scala is pure object oriented language where each value is treated as an object. Behaviours and classes of the object are described by classes and traits. Multiple Inheritance is supported by mixin based composition and extending classes by subclassing.
  • Functional: Scala is a functional programming language where each function is a value. Every value is an object so each function is an object. Scala supports Nested and Higher order functions and provisions for defining anonymous functions.
  • Statically Typed: Scala enforces type system that provides constraints and abstractions to be used in a safe and coherent manner in other words Scala is a strongly typed language. The type system support includes variance annotations, generic classes, upper and lower bounds, explicitly typed self references, views and polymorphic methods. Local Type Inference mechanism ensures that there is no need to annotate the program with redundant type information. All these together forms a powerful basis for safe reuse of programming abstractions.
  • Extensible: Scala provides language mechanism combinations that are unique and hence easier to integrate new language constructs in the form of libraries. Closures are automatically constructed upon the expected type and any method can be used as infix or postfix operators. New statements can be facilitated without extending the syntax and macro like meta programming facilities.
  • Runs on JVM: Scala runs on Java Virtual Machine(JVM) and when a scala program is compiled, byte code is generated that is executed by JVM. Both Scala and Java share a common platform and hence java programs can be converted to Scala.
  • Executes Java Code: Scala can use all the classes of Java SDK, custom java classes or Java open source projects.

Scala-Programming

Scala and Java

Scala provides some of the extra features that differ from Java. They are;

  1. Concurrency Support: Support for multiple cpu/core utilization out of the box enabling faster execution of programs unlike traditional programming languages.
  2. Closures: Closure is a function whose return value depends on the value of one or more variables defined outside this function.
  3. Traits: Trait consists of a method and field definitions that can be reused by mixing them into classes.
  4. Nested Functions: A Function defined inside another function is termed as a Nested function and is supported in Scala.
  5. Domain Specific Language support: Enables users to write their own language specifications and implement using Scala.
  6. Type Inference: Automatic detection of data types even though not specified by the user.
  7. All functions are object: It supports all standard OOPS concepts and makes functions easy to reuse.
  8. All types are Objects: Everything is an object in Scala unlike in Java.
  9. Supports REPL (Read Evaluate Print and Loop): Allows us to type the programs on the fly in a shell and get back immediate output.

Scala Hello World Program

Let us consider a simple hello world program and understand the basic structure of a scala program. This is just to get started and will go into setup and installation procedures in detail in the next article.


scala> object HelloWorld {

| def main(args: Array[String]) {

| println("Hello World!!")

| }

| }

object is the keyword, HelloWorld is the name of the object that will be created.

Scala program execution starts with main method so the main method is defined by passing string array as an argument.

def is a keyword indicating that we are defining a method whose name is “main”. We use println to print “Hello World”.

Key points about Scala Program

  • Scala Programs are case sensitive HelloWorld is different from helloWorld
  • First letter of the Class Name should be in uppercase.
  • File name should be exactly same as the object name
  • Method names should begin with a lowercase.

After typing the above program in scala shell run the program we get the message “defined object HelloWorld”

Now run the HelloWorld program as;


scala>HelloWorld.main(null)

Hello World!!

Here we invoke the main method of the HelloWorld object.

Scala Frameworks

Scala is used a lot these days especially in enterprise applications. That’s why there are frameworks getting developed on top of Scala programming language. Some of the popular frameworks include;

  • Play framework
  • Bowler framework
  • Lift framework

That’s all for brief introduction of Scala Programming Language, we will look into installation and Scala language core features in coming posts.

Comments

  1. Venu says:

    Great scala tutorials, easily understandable, I request share some tutorials on case classes its most useful in apache spark.

  2. Mohamed says:

    Hi Dev,

    Your tutorials and explanation are as usual top notch.. Could add some tutorials for grails and groovy also…

Leave a Reply

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

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages