Sunday 5 June 2011

Scala - tutorial for newbies (Part 1)

Last couple of days I have been thinking of sharing my Scala notes. I am still in learning phase. So any feedback from you will definitely help me to improve my understanding in Scala. There are many articles available in the Internet about Scala. You can read these articles to know more about Scala. In this article I will tell you how to program in Scala. There will be several parts of this tutorial. This is the first part.

A brief introduction:
  • Scala stands for "scalable language".
  • It is both object-oriented and functional.
  • Java compatible - Scala programs compile to JVM bytecode.
  • Scala program can call Java methods, access Java fields, inherit from Java classes, and implement Java interfaces - good news to Java programmers.
  • Scala is concise, less ceremonies.
  • Scala is statically typed.
  • And it is smart Java.

Please download the latest Scala installation from http://www.scala-lang.org/downloads and follow the instructions suitable for your platform. I will use Scala interpreter to try all the examples. So let start writing some Scala code. In my machine I have Scala 2.8.1.final. If you install Scala correctly and enter "scala" at a command prompt then you should see something like this:

E:\>scala
Welcome to Scala version 2.8.1.final (Java HotSpot(TM) Client VM, Java 1.6.0_23)

Type in expressions to have them evaluated.
Type :help for more information.

scala>

Probably every programmer in this planet started their programming journey by typing "Hello World!". Let me show you how you can do it in Scala.

scala> println("Hello World!")
Hello World!

scala>

I think you probably notice that I haven't typed 'System.out.println("Hello World!")' like Java. I also haven't used any semi-colon ";". By this time I believe you have understood why I said that Scala is concise and less ceremonial. You will see these things more.

Now type 2 + 3.

scala> 2 + 3
res1: Int = 5

In the above figure you will find that an automatic user-defined name "res1" is created and 5 is assigned to it. "res1" means result 1. "Int" indicates integer.

You can use "res1" identifier to do some further programming. For example:

scala> res1 * 4
res2: Int = 20

A new identifier "res2" is created and 20 is assigned to it. The value inside "res1" is not changed, it is still 5. If you type "println(res1)" at your command prompt you will see this.

scala> println(res1)
5

Now it is time to do some experiment with variables. Scala supports two kinds of variables - val and var. A val can never be reassigned if it is already assigned. So it is similar to final variable in Java. But a var can be reassigned. Try these below examples:

scala> val mymsg = "Hello to Scala"
mymsg: java.lang.String = Hello to Scala

A val variable "mymsg" of type java.lang.String is created and "Hello to Scala" is assigned to it. Now if you try to reassign it with a new value you will get an error message.

scala> mymsg = "Good Morning!!"
<console>:6: error: reassignment to val
mymsg = "Good Morning!!"
^

Now if you try the same thing with var variable you will not see any error message.

scala> var mymsg2 = "Hello to Scala"
mymsg2: java.lang.String = Hello to Scala

scala> mymsg2 = "Good Morning"
mymsg2: java.lang.String = Good Morning

If you look carefully in above variable related examples you will find that I haven't specified "java.lang.String" anywhere in my variable definition. This shows Scala's ability to figure out types if you leave off - "type inference".

Few more points before I stop this article for today:

Packages in Scala are similar to packages in Java: they partition the global namespace and provide a mechanism for information hiding.

All of Java's primitive types have corresponding classes in the Scala package.

For Example:

scala.Boolean => Java's boolean
scala.Float => Java's float

Once again thank you very much for reading this article. I hope that you enjoy it and like me you will also keep learning Scala. Any feedback is welcome.

4 comments:

  1. Cool post! It should be noted that Scala can be executed in three different ways (REPL, Script, Application) which might behave slightly different. What is shown here is Scala's REPL (Read-Eval-Print-Loop, see http://en.wikipedia.org/wiki/Read-eval-print_loop ). If you don't want to install Scala just for playing around, there is an online version availabe: http://www.simplyscala.com/

    ReplyDelete
  2. Hi Landei,

    Thank you for your feedback and helpful tips.

    Sanjoy

    ReplyDelete
  3. Nice start. I like the explanation of the automatic "resX" variables the REPL creates. When I first learned Scala I just ignored these and didn't knew I could use them in my own expressions.

    One recommendation maybe?
    A short outline at the beginning, what the reader can expect, might help diving easier into the right Scala parts of the brain.

    ReplyDelete
  4. Hi Sanjay,

    Thanks for posting very good article in very simple and straight forward way. I can say it is very nice tutorial to start Scala.

    Thanks,
    Binod Suman

    ReplyDelete