<<If you know how to write your first program using scala go to Deep dive into Scala >>
You know java, ruby, groovy, python - unlearn those momentarily if you need to learn scala (for that matter any new paradigm). Though smalltalk and haskell knowledge may help. I learned a little bit of haskell to understand what the functional world of languages looks like. Any way scala is an awesome language but without unlearning mainstream languages it may appear seemingly difficult. Lets see one example - a bad old HelloWorld. Before that just download scala from http://www.scala-lang.org/downloads. Pick up appropriate package for your OS, install it, don't forget to make it available in your system path - you know how easy it is; I am not going to explain it here. Now lets say your done - commands scala, scalac are in your system path. Issue scala
command from your terminal and you can see the scala
shell as below.
scala>
This is scala
shell. Lets write a program directly there.
scala> print("my name")
and it will display
my name
We have got the first dose of our scala - lets now see a real program
scala> object HelloWorld {
| def main(args: Array[String]) {
| println("Hello World!!!")
| }
| }
write the above code as it is written here with maintaining the lines from scala
prompt. At the end when you press your last "Return/Enter" you see
defined module HelloWorld
And then run it like this from scala
prompt
scala> HelloWorld.main(null)
And you will see
HelloWorld!!!
Cool... we saw how to write the first scala
program. The difference so far with java
or other languages that we defined object
directly. Don't worry we can define class
too. We will see that next.
scala> class HelloWorld {
| def main(args: Array[String]) {
| println("HelloWorld!!!")
| }
| }
And you see the messagedefined class HelloWorld
Lets run it now
scala>var hw = new HelloWorld
scala>hw.main(null)
You see
HelloWorld!!!
on the display
Hello world program done. We should dive deep into the theories of scala
. The values it brings, the added advantages over java
and other mainstream and contemporary languages. The notion of functional language, the traits and mix-ins, the Actor
and concurrency, the compact syntaxes etc. Tune in to my next post - coming in few hours.
One thing - obvious - forgot to mention. You can write the above to code examples in file. If the file name is HelloWorld.scala
you can compile it from your terminal shell (not scala shell) using scalac HelloWorld.scala
scala -classpath . HelloWorld
Next read Deep dive into Scala