Self-Learn Yourself Scala in 21 Blogs – #2
Blog 2 – Lets’ get started with Scala
Just type Scala in your environment to get the Scala interpreter and if everything is fine we will prompt with scala>. If you have problem with installation please follow the link, which has step by step explanations.
So we are good to explore the Scala commands. Now let’s write first Hello World program in the Scala programming.
scala> println(“Hello Scala World”)
Hello Scala World
Now in detailed way, println is a function defined in scala.Console and scala.Console uses system.out.println to print the message to the console. No need to use Console as prefix, because predef maps println to console.println. “Hello Scala World” is a string is used as a parameter and in turn Scala outputs the same string.
The Scala predef is very interesting and the Scala complier implicitly imports java.lang, the Scala package, and an object called scala. Predef to every compilation unit and every to every Scala programs.
Whenever we are giving inputs to the Scala interpreter it reads the input and evaluates and creates the type object and prints it back to console. The whole steps are called read-evaluate-print (REPL) and we will discuss more in detail in forth coming blogs.
Let us quickly list out all the vital Scala interpreter commands,
And as like every programming languages we have many Scala basic types like Byte, Short, Int, Long, Float, Double, Boolean, Char. One note on this in Scala all the type we declared will be declared using initial caps. And working with XML in Scala doesn’t need any special third-party parsers, Scala itself supports XML literals.
Below is the syntax for the Scala function declarations, (Inspired from the Scala in Action book)
Then now let us discuss what is scala.collection.immutable.$colon$colon. Scala.collection.immutable.List is an abstract class and it comes with two implementations, the one is scala.Nil Class and the other one is with scala.::. And :: in Scala is a valid identifier represents any non-empty list; Nil represents the empty list.
And to add a setter, you have to suffix your setter method with _=. package is a special object that defines a set of member classes and objects. The Scala package lets us segregate code into logical groupings or namespaces so that they don’t conflict with each other.
And one more Scalac2 is the compiler that comes bundled with the Scala distribution. If we have installed Scala as specified in the above link then we should have it available in our path. The Scala compiler provides lots of standard options, like deprecation, verbose, and classpath, and additional advanced options .
Below is the figure from Scala in Action book, for Class hierarchy of Scala with subtypes and views.
Reference – Scala in Action by Nilanjan, and Communities.
Interesting? Please subscribe to our blogs at www.dataottam.com, to keep you trendy and for future reads on Big Data, Analytics, and IoT.
As, always please feel free to suggest or comment email@example.com.