A Scala Tutorial for Java programmers
This document gives a quick introduction to the Scala language and compiler. It is intended for people who already have some programming experience and want an overview of what they can do with Scala. A basic knowledge of object-oriented programming, especially in Java, is assumed.
As a first example, we will use the standard Hello world program. It is not very fasci- nating but makes it easy to demonstrate the use of the Scala tools without knowing too much about the language. Here is how it looks:
object HelloWorld {
def main(args: Array[String]) {
println(”Hello, world!”)
}
}
The structure of this program should be familiar to Java programmers: it consists of one method called main which takes the command line arguments, an array of strings, as parameter; the body of this method consists of a single call to the predefined method println with the friendly greeting as argument. The main method does not return a value (it is a procedure method). Therefore, it is not necessary to declare a return type.
What is less familiar to Java programmers is the object declaration containing the main method. Such a declaration introduces what is commonly known as a single- ton object, that is a class with a single instance. The declaration above thus declares both a class called HelloWorld and an instance of that class, also called HelloWorld.
This instance is created on demand, the first time it is used. The astute reader might have noticed that the main method is not declared as static here. This is because static members (methods or fields) do not exist in Scala. Rather than defining static members, the Scala programmer declares these members in singleton objects
Get tutorial pdf A Scala Tutorial for Java programmers
Related Tutorial
Tags: array, astute reader, basic knowledge, command line arguments, hello world, java programmers, nating, object declaration, object oriented programming, programmer, programming experience, scala, singleton, static members
Comments
Leave a Reply