Introduction to Scala Language


Scala is one of the most popular JVM languages available today. It has been designed to scale to the future requirements and hence named as a Scalable Language (Scala). Scala is not only a fully Object Oriented Language but also a fully Functional Language. For a regular Java Programer the Scala language syntax might look magical and very concise. In this series of Online Scala Tutorials we will walk through various Scala features.





Fully Object Oriented, Fully Functional:

In Object oriented language the data & the operations that act upon the data are kept in Objects. Such  can reside as a member of another objects. It's well known that Java is not a purely Object Oriented Language. Java has primitive datatypes, which are not objects. Java also has static variable. Static variables are not bound to any object and they belong to a class that contains it, hence static variables are also not objects. Scala on the other hand is a fully object oriented language. Scala doesn't have anything primitive and static. Everything you write in Scala is an object and every operation is a method call. Even the arithmetic operators in Scala are actually methods. When you perform any arithmetic operation (using an operator), you are actually invoking a method named as that operator.

In a Functional Programming language, functions are the first class citizens. That means every function has a value, functions are be passed as arguments to other functions, functions can be returned from a function, or even a function can be defined in another function. This gives you a way to create custom literals that match you your domains.

Another feature of a functional programming language is, its function (or operations) just map input to output. That means they take some input, applies some operation and generate certain output. They do not change any data, and hence they do not have any side effects. Most of the operations on Java String are of this kind. Operations on the Java Strings do not change the underlying String object, but they would apply some operation on the String value and return the output as a new String object, keeping the original String object as is. This feature is called as immutability.

Scala supports these features of a functional programming language. All of the functions in Scala are first class elements. Scala provides us with both immutable and mutable type of programing choices.


Hello Scala:

If you are aware of a Java Programming language then you must be familiar with 'javac' & 'java' commands. Similarly Scala provides with 'scalac' & 'scala' commands. We write Scala programs in a file with a '.scala' extension, compile it with 'scalac' and execute with 'scala'.

Let's write out first Hello World program.



object HelloScala {   
   def main(args: Array[String]) {
      println("Hello, Scala..")
   }
}


To a normal Java Programer the syntax might feel familiar with little differences. The first difference is Object keyword. Remember, the Objects defined in Scala like the one above are not similar to normal Java Classes. Objects in Scala are by birth Singleton. That means you cannot create a second instance of these Objects.

Why Scala needs to have Singleton as a language construct?
The answer is, Scala doesn't have static variables. We can use Singletons as an powerful alternatives for those missing statics. If one object changes a state of any Singleton Object all the other objects would see the change. That's what we used to achieve by using static variables.

The second difference is main method doesn't have static keyword. That is because it resides in an Object and hence it is static like by default.



Variables, Values:

In Scala you can define a variable in two different ways. If you want something like final then define a val (value) else define a var (variable).  The values in Scala like final in Java. Once assigned you cannot change their value. They are immutable. Variables on the other hand have variable values.


scala> val tutName : String = "Introduction to scala"
tutName: String = Introduction to scala


At first there is a identifier whether it is a var or val. Then name of the variable. Datatype after the colon and then value assignment. If try to reassign tutName we'll get an exception.

scala> tutName = "another string"
<console>:8: error: reassignment to val
       tutName = "another string"



Let's look now what happens if we reassign a variable. See the the below example.



scala> var tutName : String = "Introduction to Scala"
tutName: String = Introduction to Scala

scala> tutName = "another String"
tutName: String = another String



In the above examples. we are explicitly telling the Scala compiler that the variable or the value we are defining is of type of String. What if we don't do that.



scala> var tutName = "Introduction to Scala" // without explicit datatype
tutName: String = Introduction to Scala      // Variable automatically created of type String


It still works! That is because of Scala's powerful Type Inference mechanism. It identifies the type of value you assign and automatically decides the type of the variable/variables. Defining a datatype is sometimes a redunduncy. When we assign a certain type of value, there is no need to put explicit type of the variable it is being assigned to. 

We are yet to dive more into the Scala features, in the coming tutorials. Stay tuned!