I am writing this blog to give you brief on Scala technology. Since past couple of days I was reading Scala and Scala based web framework Lift so just thought to share something what I did. So before starts with it, give you some brief idea what scala is?

Scala is a multi-paradigm language which runs on the JVM also gives the power of Functional Programming as well as Object Oriented programming and you can also say this language is better java with lot of utility classes, functions and all. The name Scala derived for “Scalable” and “Language” and it has been designed by Martin Oderskyvery well known for “Java Generics”.

Why Scala ? we have already good technology in the market like Java, C++, Ruby on Rails, and why we are talking about Scala technology? The popularity of Java and C++ has been decreasing against modern dynamic programming languages like Scala. These technologies provide more powerful feathers, and by providing effective syntax reducing lots of line of code.

As a Java, J2EE and Salesforce developer when I was started to learn Scala language first I started to look into comparisons between Java and Scala. Now below you will see some features provided by scala with respect to Java language. The best way to compare Scala with Java is to start with writing a simple Class. Writing a class in scala is simple and if you know how to write class in Java and C++ then you will not find any difficulty and I must say you will enjoy the syntactical freedom provided by Scala.

Now I am writing a Class called Car having two variables with one is final. Show below class for more details,

package com.interview.tests;

public class Car {

private String color;

private final int version = 1;

public Car(String color) {
this.color = color;
}

public String getColor() {
return color;
}

public void setColor(String color) {
this.color = color;
}

public int getVersion() {
return version;
}

}

In Scala if you want to write above class you just have to write two line of code,

class Car(color: String, val version: Int) {

}

Scala compiler generates the same thing which we have written in java by removing Public as class access modifier and Private as variable access modifier. In short, you get the same functionality by writing the less code, easier to read and less error prone than Java Class.

Yes I know as a developer you think if scala define constructor like this how can we define multiple constructor. Constructors in Scala are a bit different than Java. Scala has two type of Constructor.

Primary Constructor

Auxiliary Constructor

Primary Constructor As we know in Java we have a no argument constructor which is provided by every class and we don’t have to write explicitly. In scala primary constructor also comes in same way and we can define in scala as below,

class Car {
var color: string;
}

Above scala constructor doesn’t accept any parameters, but you can also define primary constructor with parameters and this

is how scala constructor differs from Java one. We can define parameter base constructer in scala as below,

class Car(val version: Int , var color: String) {

}

AuxiliaryConstructor If we want to declare more than one constructor in Java we used to overloading mechanism accepting different parameters. On similar note Scala classes declare Auxiliary constructor which are overloaded forms of the Primary one. In scala the Auxiliary constructor are named as this like,

Yes as you see you must call primary constructor before you go ahead with your Auxiliary based constructor.

You learn how to declare class now you will see how to declare variable. Like class remove access modifier same as in variable declaration you don’t need to write access modifier and variable declaration comes in two ways,

Var: using var you can declare your normal variable which comes with getter and setter method and you can change your variable value using setter and get value using getter.

Val: using val you can declare your Final type of variable which comes only with getter method you cannot set the value.

You already aware of Scala Class and variable it’s time to learn about scala member function. In scala function starts with def keyword with function name and arguments. Below you can find how to write function in scala,

Now as you see mileage function starts with def keyword, having two parameters with Double type running kilometres and petrol price which returns calculated car mileage in Double. But wait we have not written any return keyword in method, yes scala provide you the freedom where you don’t need to write return and in function last statement be your return value. Here you will find return type written after function “: Double = “. Scala also identified return type without mentioning return type at function, you just need to write “=” equals after function like,