SCALA

Scala runs on JVM. It can interoperate with Java and supports to programming models

    • OO programming

    • Functional Programming

Type inference - Means that we don't require to specify the type and the compiler will handle. Due to this compile time will be longer in scala as the project grows big.

In Scala world every thing is an expression.

A class like this can be written in a single line in Scala

ackage cashflow.java;

import java.time.LocalDateTime;

public class Cashflow {

private final double amount;

private final String currency;

private final LocalDateTime due;

public Cashflow(final double amount, final String currency, final LocalDateTime due) {

this.amount = amount;

this.currency = currency;

this.due = due;

}

public double getAmount() {

return amount;

}

public String getCurrency() {

return currency;

}

public LocalDateTime getDue() {

return due;

}

}

Like this in Scala which

class Cashflow(val amount: Double, val currency: String, val due : java.time.LocalDateTime)

If you inspect the class files created by Java class and scala class the field methods will be identical. Here scala compile is doing a lot of work, by taking a single line of code and generating and three methods. The Fundamental difference between java compiler and scala compiler is that java compiler does just the conversion of interpreter code to bite code, whereas scala generate codes and can produce a large amount of source from a small amount to code.

You can do this by using the

javap -c java/Cashflow.class

Java and JVM has two types of values - Primitive types and Object references. But in earlier languages like C++ a value like internet and be referenced as memory address and pointers. The objective of java was to completely eliminate the developers from doing this to avoid memory problems. But we do loose the capability of having control memory precisely.

In scala there are some techniques to over come this short comings. i.e by adding the key work 'case'.

Just by adding 'case' to the above scala code the size of the class files increases a lot and 2 classes are generated 'Cashflow$.class and Cashflow.class. If you examine the bite code, there would be lots of new methods created by scala in Cashflow.class. You can find how much the scala compiler does.

Case Class -Case classes are always immutable. For case class val keyword is always implied and not need to to specify 'val' keyword in the code. Case class is considered as unit.

Pre-defined function come from scala-predef

In java unit of compilation is the class where as unit of compilation is the input file. So multiple classes can live in scala file.

Strings in scala -

scala used java.lang.String. When a val is set with double quote it is considered as String. If it is single quite it takes it as Char.

Though it uses java String, there are quite a lot more which scala compiler does.

with 3 quite """ - scala considers it as continuous string.

s1: String =

"

line one

line two

line three

"

scala> s1.getClass

res2: Class[_ <: String] = class java.lang.String

Though scala does not support primitive types, but internally we can see that scala is clever to find the primitive types

scala> val n =3

n: Int = 3

scala> n.getClass

res3: Class[Int] = int

Note: when getting the getClass we are not passing the parenthesis, this is because scala assumes parenthesis if it is not passed. Though we need to pass where there are values to be passed.

TYPE SYSTEM

Java type system

This whole to java type system sits inside as a piece of type system. All the java type systems are sub type of scala called 'AnyRef' above 'AnyRef' there is 'Any'

SCALA TYPES

Scala type system not only contains ref types by also val types(which are not primitive types). And there also a type called unit type.

AnyRef - is equivalent to java.lang.Object

FUNCTIONS IN SCALA

Function can be defined by using the 'def'. You don't need to specify the return type of the function. If there is recursive function return type is required.

def square(n :Int) = n*n // no return type specified

def cube (n: int) : Int = n*n*n // we can specify the return type if needed.

def fact(n: Int): Int = n * fact(n-1) // in this factorial function - return type is required