• Call us: +91 9501707741
  • tutorialzdiary@gmail.com

Scala

Scala Hands on

First Scala Program

Scala Tutorials

Scala Introduction
What is Scala ?
Scala vs Java

Scala Installation

Scala Setup

Scala is next level of JVM language, Earning is popularity day by day against most popular sibling language JAVA. Considerable amount of developers are switching to scala and many companies are adopting it

most known player are

Twitter
LinkedIn.
Twitter.
Foursquare.
Netflix.
Tumblr.
The Guardian.
precog.
Sony

and inspiring many others . it’s future looks very bright.

Some of the more complex features of the language (Tuples, Functions, Macros, Monods ,to name a few) ultimately make it easier for the developer to write better code and increase performance by programming in Scala.

On plus side ,Scala is a type-safe JVM language that incorporates both object oriented and functional programming into an extremely concise, logical, and extraordinarily powerful language. Scala can be coded in Java way and Scala can use any Java library, which in my opinion a great decision made by designers of Scala.Since tremendous works have already been done in the form of open source framework and library in Java, it’s best to reuse them, rather than creating a separate set for Scala.

This article main focus on the advantages of Scala, especially versus Java (since Scala is written to run in the JVM).Scala was created specifically with the goal of being a better language, shedding those aspects of Java which it considered restrictive, overly tedious, or frustrating for the developer. As a result, there are indeed code distinctions and paradigm shifts that can make early learning of Scala programming a bit more difficult, but the result is a much cleaner and well organised language that is ultimately easier to use and increases productivity.

Out of several differences, one of the main difference between Scala and Java is its ability to take advantage of Functional programming paradigm and multi-core architecture of current CPU.Since current CPU development trend is towards adding more cores, rather than increasing CPU cycles, it also favors functional programming paradigm.

Apart from the functional programming aspect, there are many other differences as well. One of the obvious ones is improved readability and succinct code.

Vote for Clean short code

Java is old has large community base ,it has contributed to its complexity. Anything can be coded in Java,but lines of code required to do so is formidable.Programming in Scala, on the other hand, has a slightly more complex structure.

Java is old has large community base ,it has contributed to its complexity. Anything can be coded in Java,but lines of code required to do so is formidable.Programming in Scala, on the other hand, has a slightly more complex structure.

Compare, for example, this simple “Hello World!” program in Java vs. Scala:

Hello World in Java:

public class HelloJava {
public static void main(String[] args) {
System.out.println(“Hello World!”);
}
}

Hello World in Scala:

object HelloScala {
def main(args: Array[String]): Unit = {
println(“Hello World!”)
}
}

Scala is less verbose even in this simple example.

For a more practical example, let’s take a look at creating a simple list of Strings:

Java:

List list = new ArrayList();
list.add(“1”);
list.add(“2”);
list.add(“3”);

Scala:

val list = List(“1”, “2”, “3”)

Certainly there are some tricks in Java to shorten the code a bit, but not in standard usage.

Now consider a case where we have a list of strings that are numbers, but we want to convert that list to a list of integers:

Java:

List ints = new ArrayList();
for (String s : list) {
ints.add(Integer.parseInt(s));
}

Scala:

val ints = list.map(s => s.toInt)

Thanks to Scala’s functional properties, this conversion becomes extremely simple.

Java

public class Person
{
private String firstName;
private String lastName;
String getFirstName() { return firstName; }
void setFirstName(String firstName) { this.firstName = firstName; }
String getLastName() { return lastName; }
void setLastName(String lastName) { this.lastName = lastName; }
int hashCode() ….
boolean equals(Object o) { …. }
}

Scala

case class Person(firstName:String, lastName:String)

Lambda Expressions

Java

list.sort((x,y)-> {
int cmp = x.lastName.compareTo(y.lastName);
return cmp!=0 ? cmp : x.firstName.compareTo(y.firstName)
}

Scala

list.sort((x,y) => {
val cmp = x.lastName.compareTo(y.lastName)
if (cmp!=0) cmp else x.firstName.compareTo(y.lastName)
}

We can see that the code is really similar, but:

Scala

var (maxFirstLen, maxSecondLen) = (0,0)
list.foreach{
x => maxFirstLen = max(maxFirstLen, x.firstName.length)
maxSecondLen = max(maxSecondLen, x.secondName.lenght)
}

Java

[?] (it is impossible to modify the content the lambda expression has been called from).

Thus, lambda expressions in Java are syntactic sugar over anonymous classes that have access to the final objects of a context only. But in Scala they are full-onclosures that have the full access to the context.

Wrap-up
Hopefully, this article stacks up Java vs. Scala enough at introductory level to give you a preliminary sense of the power and capabilities of Scala and whets your appetite for learning the language.

I don’t want to discuss banal things like compile time, learning curve, and community size. All of us know that Java compiles faster, it’s easier to learn it, and start to use it. Finally, the number of Java developers is much higher than Scala devs. But what is interesting in all these boring questions? Scala knows how to resolve issues with continuous improvements.

As you can see, Scala makes a good business, especially if we look at it in dynamic:

scala_vs_java

This diagram reflects the demand on Scala and Java developers. Please note that curves are relative, not absolute. It means that the total number of Java positions still exceed Scala positions. It’s easy to predict Scala’s progress in the future. Why? Because Scala totally satisfies its users and as a result – customers. Don’t forget about the Big Data world. Spark acquired total domination there. Reactive programming is represented by Akka. Actually, I like this cozy company.

Scala is not only great language that can make programming less tedious and more enjoyable, but it’s also being used by some of the largest companies in the world (LinkedIn, Twitter, FourSquare, The Guardian, to name just a few).

The popularity and usage of Scala is rapidly on the rise, as evidenced by the ever-increasing number of open positions for Scala developers. If you haven’t already done so, now would be a good time to begin riding the wave and stop asking “Why learn Scala?”

image_pdfimage_print

Leave a reply

Your email address will not be published. Required fields are marked *


Newsletter
Training Enquiry