Scala — Basics :-

Lavlesh Singh
Analytics Vidhya
Published in
4 min readOct 9, 2020

In this section, we will go over the basic foundation of the Scala programming language. Throughout the blog we will use the popular IntelliJ IDEA which we’ve setup in my previous post. I hope that by now you are more comfortable with using IntelliJ IDEA. If not, feel free to review my previous post.

If you have a good understanding on Java, then it will be very easy for you to learn Scala. The biggest syntactic difference between Scala and Java is that the ‘;’ line end character is optional.

Case Sensitivity − Scala is case-sensitive, which means identifier Hello and hello would have different meaning in Scala. please go through below for more differences.

  • Class Names − For all class names, the first letter should be in Upper Case. If several words are used to form a name of the class, each inner word’s first letter should be in Upper Case.

Example − class MyFirstScalaClass.

  • Method Names − All method names should start with a Lower Case letter. If multiple words are used to form the name of the method, then each inner word’s first letter should be in Upper Case.

Example − def myMethodName()

  • Program File Name − Name of the program file should exactly match the object name. When saving the file you should save it using the object name (Remember Scala is casesensitive) and append ‘.scala’ to the end of the name. (If the file name and the object name do not match your program will not compile.

Example − Assume ‘HelloWorld’ is the object name. Then the file should be saved as ‘HelloWorld.scala’.

  • def main(args: Array[String]) − Scala program processing starts from the main() method
  • Object − Objects have states and behaviors. An object is an instance of a class. Example − A dog has states — color, name, breed as well as behaviors — wagging, barking, and eating.
  • Class − A class can be defined as a template/blueprint that describes the behaviors/states that are related to the class.
  • Methods − A method is basically a behavior. A class can contain many methods. It is in methods where the logics are written, data is manipulated and all the actions are executed.
  • Fields − Each object has its unique set of instance variables, which are called fields. An object’s state is created by the values assigned to these fields.
  • Closure − A closure is a function, whose return value depends on the value of one or more variables declared outside this function.
  • Traits − A trait encapsulates method and field definitions, which can then be reused by mixing them into classes. Traits are used to define object types by specifying the signature of the supported methods.
  • def keyword — “def” keyword is used to declare a function in Scala.
  • Function_name — It should be valid name in lower camel case. Function name in Scala can have characters like +, ~, &, –, ++, \, / etc.
  • Parameter_list — In Scala, comma-separated list of the input parameters are defined, preceded with their data type, within the enclosed parenthesis.
  • Return_type — User must mention return type of parameters while defining function and return type of a function is optional. If you don’t specify any return type of a function, default return type is Unit which is equivalent to void in Java.
  • Method body — Method body is enclosed between braces { }. The code you need to be executed to perform your intended operations.

def function_name ([parameter_list]) : [return_type] = {

// function body

}

Note - If the user will not use the equals sign and body then implicitly method is declared abstract.

  • Define some variables — Scala has two kinds of variables, vals and vars. A val is similar to a final variable in Java. Once initialized, a val can never be reassigned. A var, by contrast, is similar to a non-final variable in Java. A var can be reassigned throughout its lifetime.
    Here’s a val definition:

scala> val msg = “Hello, world!”

This statement introduces msg as a name for the string “Hello, world!”.
The type of msg is java.lang.String, because Scala strings are implemented
by Java’s String class. If you’re used to declaring variables in Java, you’ll notice one striking difference here: neither java.lang.String nor String appear anywhere in the val definition. This example illustrates type inference, Scala’s ability to figure out types you leave off.

Define some functions :- Now that you’ve worked with Scala variables, you’ll probably want to write some functions. Here’s how you do that in Scala:

def max(x: Int, y: Int): Int = {

if (x > y) x

else y

}

max: (Int,Int)Int

function’s result type is an equals sign and pair of curly braces that contain
the body of the function. In this case, the body contains a single if expression,
which selects either x or y, whichever is greater, as the result of the
max function. As demonstrated here, Scala’s if expression can result in a
value, similar to Java’s ternary operator. For example, the Scala expression
“if (x > y) x else y” behaves similarly to “(x > y) ? x : y” in Java.

  • Write some Scala scripts:- Although Scala is designed to help programmers build very large-scale systems,
    it also scales down nicely to scripting. A script is just a sequence of
    statements in a file that will be executed sequentially. Put this into a file
    named hello.scala:

println(“Hello, world, from a script!”)

I hope you enjoyed Scala Basics clearly, if still having any doubt you can ask me anytime.

Thanks for reading !! my post.

i’ll meet you again with next interesting topic of Scala..

--

--