Why Scala ?
Scala is a programming language, other than programming tasks you can use it to write scripts.
The name Scala stands for "scalable language.", but I would prefer to call it Java ++ , reason being it runs on the standard Java platform (JVM) and interoperates seamlessly with all Java libraries, additionally it has many rich features which are still not available in Java.
Today we will discuss why Scala is a improved version of Java:-
First I would suggest you to check: https://www.toptal.com/scala/why-should-i-learn-scala
If still not convinced then keep reading :
Scala allows growing new types & control structure and it looks more native, for example in below example which looks more natural (click the image below) :-
Through implicit conversion Scala not only supports Java libraries but also "dresses them up" to make them nicer. For instance, Scala's strings support methods like toInt or toFloat, which convert the string to an integer or floating-point number. So you can write str.toInt instead of Integer.parseInt(str). In above example Scala wrapped java.math.BigInteger and made it more natural then then writing x.multiply(factorial(x.subtract(BigInteger.ONE)))
No doubt Java has rich thread-based concurrency library but threading model is built around shared memory and locking, a model that is often difficult to reason about, especially as systems scale up in size and complexity. It is hard to be sure you don't have a race condition or deadlocks —something that didn't show up during testing, but might just show up in production.
Along with Java's rich thread-based concurrency library. Scala programmer can use Akka which works on message passing architecture. Actors in Akka communicate through sending and receiving messages, every Actor has own message box where incoming messages are queued.
Check this website for simple Hello world program to understand how easy it to use Akka: https://alvinalexander.com/scala/simple-scala-akka-actor-examples-hello-world-actors/
Scala is an object-oriented language in pure form: every value is an object and every operation is a method call. For example, when you say 1 + 2 in Scala, you are actually invoking a method named + defined in class Int. You can define methods with operator-like names that clients of your API can then use in operator notation.
For example to add new value in a map, you need to use capital = capital+ ("Japan" -> "Tokyo") or in short use +=
var capital = Map("US" -> "Washington", "France" -> "Paris")
capital += ("Japan" -> "Tokyo") // Easy to add new element right ?
Scala is also a full-blown functional language. Functions are first-class values. In a functional language, a function is a value of the same status as, say, an integer or a string. You can pass functions as arguments to other functions, return them as results from functions, or store them in variables. You can also define a function inside another function, just as you can define an integer value inside a function. And you can define functions without giving them a name, sprinkling your code with function literals as easily as you might write integer literals like 42.
Main idea of functional programming is that the operations of a program should map input values to output values rather than change data in place. The Scala libraries define many more immutable data types on top of those found in the Java APIs. For instance, Scala has immutable lists, tuples, maps, and sets. Scala encourage for referential transparency, which means that for any given input the method call could be replaced by its result without affecting the program's semantics. This helps to cache method results for intensive calls.
Scala allows default value in function parameters: https://alvinalexander.com/scala/how-to-set-default-values-method-parameters-in-scala/
Scala is less verbose and high level language, to give you an example let's say I want to find out whether or not my String contains an upper case character, which implementation looks better :
// Java prior to Java 8
boolean nameHasUpperCase = false;
for (int i = 0; i < name.length(); ++i) {
if (Character.isUpperCase(name.charAt(i))) {
nameHasUpperCase = true;
break;
}
}
// Java 8
boolean nameHasUpperCase =
name.chars().anyMatch(
(int ch) -> Character.isUpperCase((char) ch)
);
//Scala
val nameHasUpperCase = name.exists(_.isUpper)
To summarize , Scala is a scalable, compatible with Java, concise, high level (Good for complex systems), statically typed language.