Scala for the newbie: The Basics

Prayansh Ratan
4 min readJul 27, 2021

--

Scala documentation defines itself as a modern multi-paradigm programming language designed to express common programming patterns in a concise, elegant, and type-safe way. It seamlessly integrates features of object-oriented and functional languages.

Scala source code can be compiled to Java bytecode and run on a Java virtual machine (JVM). Scala provides language interoperability with Java so that libraries written in either language may be referenced directly in Scala or Java code.

The Scala Interpreter:

Steps to get started with the Scala interpreter:

  1. Install Scala
  2. Make sure that you have saved the path of the Scala/bin folder in your system PATH
  3. Open the Command Shell
  4. Type scala and hit the enter key

You’ll see something like:

Now, you may want to solve mathematical queries or may want to join strings. Those kinds of operations can be performed here.

here res0:Int , res1:String and res2:String denote the result of the subsequent commands and their type.

These results are stored for the particular session as variables and can be used as and when needed:

Also, if you’re a C++ or a Java guy cribbing about the syntax, then yes, a semicolon is not required in Scala. Those are only required if multiple statements are present on the same line. No more missing semicolon errors, Ehh?

Declaring variables and values:

There are two types of variables defined in Scala. These are var and val . val defines an immutable name for the right side content which is evaluated immediately (evaluated by value) while var defines a mutable name for the right side content which can be reassigned as and when needed.

Now, there is a third way to assign a variable name. Surprise!

def defines an immutable name for the right side content which is evaluated lazily (evaluated by name) as shown:

Notice the difference between how val and def are evaluated. def prints the value only when it’s called while val shows the value when it’s assigned as age: Int = 24

In Scala, it is always encouraged to use val rather than var unless you’re in need to modify the variables.

Types in Scala

Scala has 7 numeric types: Byte, Char, Short, Int, Long, Float, Double, and also a Boolean Type. All these types are classes. Hence, methods can be called on numbers, just hit tab after typing any number and period (.) and you’ll see all the methods that are available for the class. Here for example the Int class:

Just check out any methods that you want:

As you can see above the Arithmatic operators (+, -, *, /, %, **), Relational operators (==, !=, >, <, >=, <=), and Bitwise operators (&, |, <<, >>, >>>, ^) are present in the list of method names. This is because these operators are actually methods. So, the commands that you see, for instance a+b is actually the method + being called on a and passing b as a parameter. a+b is just a shorthand for a.+(b) So, in general a method b is just a.method(b) where method takes two parameters. Call the methods whichever way you feel comfortable and easy to understand.

Hope this was helpful. Thus we finish the basics of Scala!

--

--

Prayansh Ratan
Prayansh Ratan

Responses (1)