Open In App

Introduction to Scala

Last Updated : 24 Sep, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

Scala is a general-purpose, high-level, multi-paradigm programming language. It is a pure object-oriented programming language which also provides the support to the functional programming approach. There is no concept of primitive data as everything is an object in Scala. It is designed to express the general programming patterns in a refined, succinct, and type-safe way. Scala programs can convert to bytecodes and can run on the JVM(Java Virtual Machine). Scala stands for Scalable language. It also provides the Javascript runtimes. Scala is highly influenced by Java and some other programming languages like Lisp, Haskell, Pizza, etc.

Evolution of Scala:
Scala was designed by the Martin Odersky, professor of programming methods at École Polytechnique Fédérale de Lausanne (EPFL) in Switzerland and a German computer scientist. Martin Odersky is also the co-creator of javac (Java Compiler), Generic Java, and EPFL’s Funnel programming language. He started to design the Scala in 2001. Scala was first released publicly in 2004 on the Java platform as its first version. In June 2004, Scala was modified for the .Net Framework. Soon it was followed by second version i.e. (v2.0) in 2006. At JavaOne conference in 2012, Scala was awarded as the winner of the ScriptBowl contest. From June 2012, Scala doesn’t provide any support for .Net Framework. The latest version of scala is 2.12.6 which released on 27-Apr-2018.

Why Scala?

Scala has many reasons for being popular among programmers. Few of the reasons are :  

  • Easy to Start: Scala is a high level language so it is closer to other popular programming languages like Java, C, C++. Thus it becomes very easy to learn Scala for anyone. For Java programmers, Scala is more easy to learn.
  • Contains best Features: Scala contains the features of different languages like C, C++, Java, etc. which makes the it more useful, scalable and productive.
  • Close integration with Java: The source code of the Scala is designed in such a way that its compiler can interpret the Java classes. Also, Its compiler can utilize the frameworks, Java Libraries, and tools etc. After compilation, Scala programs can run on JVM.
  • Web – Based & Desktop Application Development: For the web applications it provides the support by compiling to JavaScript. Similarly for desktop applications, it can be compiled to JVM bytecode.
  • Used by Big Companies: Most of the popular companies like Apple, Twitter, Walmart, Google etc. move their most of codes to Scala from some other languages. reason being it is highly scalable and can be used in backend operations.

Note: People always thinks that Scala is a extension of Java. But it is not true. It is just completely interoperable with Java. Scala programs get converted into .class file which contains Java Byte Code after the successful compilation and then can run on JVM(Java Virtual Machine). 

Beginning with Scala Programming

Finding a Compiler: There are various online IDEs such as GeeksforGeeks IDE, Scala Fiddle IDE, etc. which can be used to run Scala programs without installing.

Programming in Scala: Since the Scala is a lot similar to other widely used languages syntactically, it is easier to code and learn in Scala. Programs can be written in Scala in any of the widely used text editors like Notepad++, gedit, etc. or on any of the text-editors. After writing the program, save the file with the extension .sc or .scala

For Windows & Linux: Before installing the Scala on Windows or Linux, you must have Java Development Kit(JDK) 1.8 or greater installed on your system. Because Scala always runs on Java 1.8 or above. 
In this article, we will discuss how to run the Scala programs on online IDE’s. 

Example : A simple program to print Hello Geeks! using object-oriented approach.

Scala




// Scala program to print Hello, Geeks!
// by using object-oriented approach
 
// creating object
object Geeks {
 
// Main method
def main(args: Array[String])
{
     
    // prints Hello, Geeks!
    println("Hello, Geeks!")
}
}


Output: 

Hello, Geeks!

Comments: Comments are used for explaining the code and are used in a similar manner as in Java or C or C++. Compilers ignore the comment entries and do not execute them. Comments can be of a single line or multiple lines. 

  • Single line Comments: 
    Syntax:
// Single line comment
  • Multi line comments:
    Syntax: 
/* Multi-line comments
   syntax */

object Geeks: object is the keyword which is used to create the objects. Here “Geeks” is the name of the object.
def main(args: Array[String]): def is the keyword in Scala which is used to define the function and “main” is the name of Main Method. args: Array[String] are used for the command line arguments.
println(“Hello, Geeks!”): println is a method in Scala which is used to display the string on console.

Note: There is also functional approach that can be used in Scala programs. Some Online IDE doesn’t provide support for it. We will discuss it in upcoming articles.

Features of Scala

There are many features which makes it different from other languages. 

  • Object- Oriented: Every value in Scala is an object so it is a purely object-oriented programming language. The behavior and type of objects are depicted by the classes and traits in Scala.
  • Functional: It is also a functional programming language as every function is a value and every value is an object. It provides the support for the high-order functions, nested functions, anonymous functions, etc.
  • Statically Typed: The process of verifying and enforcing the constraints of types is done at compile time in Scala. Unlike other statically typed programming languages like C++, C, etc., Scala doesn’t expect the redundant type information from the user. In most cases, the user has no need to specify a type.
  • Extensible: New language constructs can be added to Scala in form of libraries. Scala is designed to interpolate with the JRE(Java Runtime Environment).
  • Concurrent & Synchronize Processing: Scala allows the user to write the codes in an immutable manner that makes it easy to apply the parallelism(Synchronize) and concurrency.
  • Run on JVM & Can Execute Java Code: Java and Scala have a common runtime environment. So the user can easily move from Java to Scala. The Scala compiler compiles the program into .class file, containing the Bytecode that can be executed by JVM. All the classes of Java SDK can be used by Scala. With the help of Scala user can customize the Java classes.

Advantages:  

  • Scala’s complex features provided the better coding and efficiency in performance.
  • Tuples, macros, and functions are the advancements in Scala.
  • It incorporates the object-oriented and functional programming which in turn make it a powerful language.
  • It is highly scalable and thus provides a better support for backend operations.
  • It reduces the risk associated with the thread-safety which is higher in Java.
  • Due to the functional approach, generally, a user ends up with fewer lines of codes and bugs which result in higher productivity and quality.
  • Due to lazy computation, Scala computes the expressions only when they are required in the program.
  • There are no static methods and variables in Scala. It uses the singleton object(class with one object in the source file).
  • It also provides the Traits concept. Traits are the collection of abstract and non-abstract methods which can be compiled into Java interfaces.

Disadvantages:  

  • Sometimes, two approaches make the Scala hard to understand.
  • There is a limited number of Scala developers available in comparison to Java developers.
  • It has no true-tail recursive optimization as it runs on JVM.
  • It always revolves around the object-oriented concept because every function is value and every value is an object in Scala.

Applications: 

  • It is mostly used in data analysis with the spark.
  • Used to develop the web-applications and API.
  • It provide the facility to develop the frameworks and libraries.
  • Preferred to use in backend operations to improve the productivity of developers.
  • Parallel batch processing can be done using Scala.

 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads