Scala language introduction
Scala is yet another language that runs on JVM. We already have so many JVM languages, do we need one more?
What differs Scala with other JVM languages and what we can take advantage of it?
Some keywords about Scala: object, functional, static typed. This is an unusual combination. Most functional languages are dynamic typed. Many object oriented language don't support functional programming.
Looks like Scala is a multi paradigm programming language, similar to Python, but Python is dynamically typed.
Hello World in Scala
Go to Download Scala and grab the msi package. Install it. Open your cmd window.
C:\Windows\System32>scala Welcome to Scala version 2.10.2 (Java HotSpot(TM) Client VM, Java 1.7.0). Type in expressions to have them evaluated. Type :help for more information. scala> print ( "Hello World" ) Hello World scala> print ( 'Hello World' ) <console>:1: error: unclosed character literal print ( 'Hello World' ) ^ scala> exit warning: there were 1 deprecation warning(s); re-run with -deprecation for details
Oops, Scala don't support single quoted string.
You can put the code in file, usually the file extension is 'scala'. Create a text file with any code editor you like. Hello.scala.
print ( "Hello World" )
You can run it directly with "scala Hello.scala". If you want to define class, you have to compile it with scalac like in Java.
Compile and run.
scalac HelloClass.scala scala -classpath . HelloClass
Scala object
For any object oriented language, the first question is what is an object. The C++ object and Java object are quite different.
Both in C++ and Java, the primitives are not object, like int, float, double. They are not "pure" OOP language. Scala is pure object oriented: every value is object.
One thing to remember about scala object is that there is no static method. Static member contradict with pure OO design. Instead, Scala use singleton object to replace static member.
object Singleton { def func ( msg: String ) = { print ( msg ) } } Singleton.func( "Hello" )
You can call the method without new operator.
Functional programming
Those features makes Scala a multi paradigm programming language.