Scala App Trait

Scala contains a trait called “App” and its used to turn objects into executable programs.

We do not need to explicitly define “main” method in a Scala object, instead the whole class body becomes the main method.

Refer the below example,

ScalaMainApp

In the above code, we define an object “ScalaMain” and in the body of that object, we define a function “add” for adding two numbers. The whole code section becomes the main method.

Java 9: Immutable List, Set and Map

Java 9  provides a convenient way to create an immutable collections such as List, Set and Map. In this post, we are going to see that with few examples.

Prior to Java 9, To create an immutable collections, we have to follow the below approach. It works fine but its verbose.

ImmutableJava8

Now, in Java 9, we can easily, create an immutable list with the List.of() method.

The List.of method returns an instance of “java.util.ImmutableCollections.ListN” [extends AbstractImmutableList]. As this class extends AbstractImmutableList, the returned list is an immutable list, so trying to update it, will throw UnsupportedOperationException.

ImmutableList.JPG

We can follow the same way for creating an immutable Set and Map. Refer the below examples.

The Set.of method returns an instance of “java.util.ImmutableCollections.SetN” [extends AbstractImmutableSet]. Its immutable so trying to update it, will throw UnsupportedOperationException.

The Map.of method returns an instance of “java.util.ImmutableCollections.MapN” [extends AbstractImmutableMap]. Its immutable so trying to update it, will throw UnsupportedOperationException.

ImmutableSet.JPG

ImmutableMap.JPG

Scala Case Class

Scala class classes are like regular classes but with a few differences.  Those are given below.

Differences:

  • When we create a case class, it also creates a companion object implicitly with apply and unapply methods.
  • By default, Case classes are serializable.
  • Case class instances can be created without using the new keyword.  By default, the apply method of case class companion object will be called and it creates an instance of case classes.
  • Suitable for modeling immutable data.
  • Case classes automatically define the toString, equals and hashCode methods.
  • Case class constructor parameters are public val’s. So we can’t modify the values once it’s assigned. Because those parameters are immutable or constant.
  • Case classes automatically define the getter methods for constructor arguments.
  • It can be used in Pattern Matching operation.
  • It contains an implicit ‘copy’ which is used for creating a copy of a case class instance. We can also optionally change the constructor arguments.
  • Scala case classes can’t be extended by other case class.

CaseClass.JPG

Scala val, var and def

In this post, we are going to see the difference between var, val and def keywords of Scala.

Var –  If you want to create a mutable variable in Scala, then you have to use var keyword. For example, you want to create a variable and the value of that variable will be changed, then you need to use var keyword to define a mutable variable.

Val –  If you want  to create an immutable variable, then you have to use val keyword. Its like creating a constant value so once the value is assigned to a variable, then its never going to change. Even if you do, then you would get a compilation error.

def – To define a function, then you have to use this keyword.

Refer the below example, to know how to use these keywords.

val keyword usage:

In the below example, we use the ‘def’ keyword to define a function. We create a variable with ‘val’ keyword and then try to modify the value once its assigned. So when you do that, then you would get a compilation error.

ScalaExampleWithVal

var keyword usage:

ScalaExampleWithVar