Scala is a pure object-oriented language. Every value in Scala is an object and every operation is a method call. So when you type 2 + 3 in your Scala interpreter you are actually invoking a method named "+" defined in class Int. With this little bit of Scala knowledge we will try to write and compile our first Scala program.
object HelloWorld { def main (args: Array[String]) { println("Hello World") } }
Please type the above program in your favorite notepad, save it and give it a name, say HelloWorld.scala. Now it is time to compile the program. We will use scalac for this purpose. Execute the below command in your command prompt:
E:\>scalac HelloWorld.scala E:\>
This will generate few class files in your current directory. One of them will be HelloWorld.class. It contains a class which can be directly executed using the scala command.
E:\>scala HelloWorld E:\>Hello World
If you have done little bit of Java programming before then the above program is not difficult to understand. This program consists of a main method that takes the command line arguments, an array of Strings, as parameter. The main method does not return any value. For this reason there is no declaration of return type. It has a single call to the predefined method println with the friendly greeting as argument.
There are few things to notice in the above program. One of them is object declaration. This declaration introduces singleton object - a class with a single instance. So the above declaration declares both a class called HelloWorld and an instance of that class, also called HelloWorld. The next thing to notice - the main method is not declared as static. In Scala, static members (methods or fields) do not exist. You need to declare these members in singleton objects.
By this time I think you know how to define a function. In Scala, function definition starts with def, then the function's name followed by a comma-sperated list of parameters in parentheses.
Lets try the below example in your scala interpreter.
scala> def min(x: Int, y: Int): Int = { | if( x < y ) x | else y | } min: (x: Int,y: Int)Int
In the above example, the function named min takes two parameters, x and y, both of type Int. You might have noticed that after the closing parenthesis of min's parameters list there is another : Int type annotation. This defines the result type of the min function. In Java, the type of the value returned from a method is its return type. In Scala it is called result type.
Did you see the equals sign in the above function? It says that a function defines an expression that results in a value. Since the above function contains only one statement, you can leave off the curly braces. It can be written like:
scala>def min2(x: Int, y: Int) = if (x < y) x else y min2: (x: Int,y: Int)Int
There may be situation when you need to define a method that takes no parameter and returns no value. Here is an example:
scala> def sayHelloToScala() = println("Hello Scala") sayHelloToScala: ()Unit
Scala interpreter has responded with sayHelloToScala: ()Unit. Here sayHelloToScala is the function's name. The empty parentheses indicate that this function does not take any parameter. Unit is the result type of this function. A result type of Unit indicates function does not return any value. It is similar to Java's void type.
With this I stop for now. Thank you very much for reading it. Please give your feedback.
Nice tutorial, Sanjoy! Very clearly written.
ReplyDelete