Blog

Filter posts by Category Or Tag of the Blog section!

Getting started with Scala

Monday, 07 January 2019

I heard about Scala programing language a few days ago; its advantages and power. After reading a few hours about the features of this cool language, I decided to write a short post about to introduce it. Scala is one of the new languages that is based on the JVM and it's most famous because of its inherent strength. Scala is influenced by Java and Its syntax has a lot of similarities to Java and that's why most of the developers easily switch to Scala. Another advantage of Scala is that it supports the object-oriented, and functional style of programming; so it can be used for any sort of application.

As I'm not familiar a lot with Scala, based on what I read, it takes less time to code as compared to Java and it is easy to write, compile, debug and run the program in Scala. You can see the Language keyword in the table below:

 

abstract

case

catch

class

def

do

else

extends

false

final

finally

for

forSome

if

implicit

import

lazy

match

new

Null

object

override

package

private

protected

return

sealed

super

this

throw

trait

Try

true

type

val

Var

while

with

yield

 

-

:

=

=>

<-

<:

<%

>:

#

@

 

 

 

Just like many languages, a lot of keywords are the same. But definitely, there are some new keywords! Now let's get our hands a little bit dirty with code. By the way, one of the most famous IDE for Scala is IntellijIdea or you can use Scastie as an online Editor. Defining class in Scala is Just like C# or Java, Classes contain methods, values, variables, types, objects, traits:

 

class Sample

val sample = new Sample

 

If you would want to create a constructor for the above class, it's a little bit different from C# or Java:

 

class Sample(var a: Int = 0, var b: string = 0)

val sample1 = new Point 

val sample2 = new Point(1,"Ehsan")

println(sample1.a)

println(sample2.b)

 

Method Definition is so simple :

 

 def add(a: Int, a: Int): Int = (a + b)*2  //the definition

 println(add(1, 2)) //the usage

 

As I mentioned before, Scala is support for functional programming as well. Functions in Scala are like the expression in C# which takes the argument:

 

val zero = () => 1

val one = (a: Int) => a + 1

val two = (a: Int, b: Int) => a + b + 1

Category: Software

Tags: Scala

comments powered by Disqus