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 and Java
Scala provides some of the extra features that differ from Java. They are;
- Concurrency Support: Support for multiple cpu/core utilization out of the box enabling faster execution of programs unlike traditional programming languages.
- Closures: Closure is a function whose return value depends on the value of one or more variables defined outside this function.
- Traits: Trait consists of a method and field definitions that can be reused by mixing them into classes.
- Nested Functions: A Function defined inside another function is termed as a Nested function and is supported in Scala.
- Domain Specific Language support: Enables users to write their own language specifications and implement using Scala.
- Type Inference: Automatic detection of data types even though not specified by the user.
- All functions are object: It supports all standard OOPS concepts and makes functions easy to reuse.
- All types are Objects: Everything is an object in Scala unlike in Java.
- 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.
1 2 3 4 5 6 7 |
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;
1 2 3 4 |
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.