Scala runs on the JVM, so Java and Scala stacks can be freely mixed. You can call Java libraries from Scala. Having said this, it is very important that you learn to write code the Scala way, and Not Java way. Currently, Java programmers with Scala experience are paid more and in demand.
Step 1: Download the latest version of Scala from “http://www.scala-lang.org/”. I downloaded scala-2.11.6 for windows 8 and installed it in “C:\development\scala-2.11.6” same location where my Java 8.
Step 2: Now, if you check your environment variable “path”, it should have “C:\development\scala-2.11.6\bin”. Open a DOS environment, and type
1 2 3 |
scala -version |
Step 3: Type “scala” on the command prompt to get the “REPL” shell, which stands for Read-Eval-Print-Loop.
1 2 3 4 5 6 |
c:\somefolder>scala scala> print("Hello scala") Hello scala scala>:q |
“:q” to quit the Scala shell. You can practice Scala on this REPL.
Step 4: Entering multi-line commands on REPL by starting with “:paste” and “ctrl+d”
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
scala> :paste // Entering paste mode (ctrl-D to finish) val number1:Int = 5 val number2:Int = 5 val sum = number1 + number2 // Exiting paste mode, now interpreting. "(ctrl+d) pressed" number1: Int = 5 number2: Int = 5 sum: Int = 10 scala> |
Step 5: In the above example, the number1 type was declared as an “Int”. In Scala the data types can be inferred as shown below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
scala> :paste // Entering paste mode (ctrl-D to finish) val number1 = 5 val number2 = 5 val sum = number1 + number2 // Exiting paste mode, now interpreting. number1: Int = 5 number2: Int = 5 sum: Int = 10 scala> |
So, REPL is a great way to learn the Scala core concepts to write code the Scala way with free online tutorials and YouTube videos. Search for “Scala tutorials for Java developers“. Learn the concepts like type inference, immutability, functional programming, expressions, pattern matching, case classes, partial function, Option type to prevent NullPointerExceptions, reactive programming, multiple parameter sets, etc.
I will focus more on industrial strength tutorials in Scala with Akka, Spark, reactive programming, etc that empowers you to target low latency and Big Data jobs.