8 differences between Scala and Java 8

1. Scala – functional programming language (Functional Language)

"Write less, do more"

For example , your application needs to select an appropriate number of words (string) that meet your criteria from a list of words – List (). To implement this logic in Java (7, 6), the syntax will take the form:

I have just taken 3 steps to get the desired results. Let's return the same result but in Scala, the steps will be shorter with lambda expressions :

In the above example, the way Java states is: "It is necessary to create a storage space for filtering data, filtering each word in the sample space and if any word that satisfies the condition starting with A is saved" . Scala said, "Filtering all the words from which the condition begins with the letter A will be saved."

In my opinion, it is undeniable that Java is a literal explanation language that is a very clear problem. But let's see how Scala is interpreted to be quite understandable and somewhat concise.

In Java 8 supporting lambda , the code can be rewritten as follows:

Although writing code lines against Java (7, 6) is less, compared to Scala it still seems too long.

2. Monads

I temporarily translate it as 'monad' or 'monolithic'. This is a concept for functional programming from mathematics. This concept seems to be difficult to understand, but it is not difficult to learn, please see below.

We will talk about the benefits of programming monads, and new learners will meet the most popular monads in Scala:

2.1 "Maybe" M monad.

Maybe monad is often referred to as an "Optional types", it implements an Option [T] . Option [T] defines two states: one is Some [T] (when the actual value of T exists), two is None if Option is empty.

In Scala we will encounter a declaration:

In Java you will need to check if (if) exists then append the value T (else), then add it to None. But Option has replaced you with this test.

Scala also offers several other methods:

2.2 Try

How it works in Scala is meaning Java.

Try also has two states: Success, when your code block does not generate an exception; Failure when there is an error in the code block in Try. Let's see it

2.3 Future

This is one of the very interesting things, it opens up a new space in asynchronous programming. This is also one of many reasons why Scala has become a strong language. For example :

Executing each of the code commands in Future, in turn, results in an A backgound task, followed by a B and then a Future result. But let's see what happens when I add a Thread inside Future:

Then backgound the task of printing the letter A, return the results of Future and then wait for the 5s to print the letter C. It should have been printed out before printing the Future results. This proves that the incompatibility has occurred, ie the main Thread continues to operate without waiting for the Sub Thread in Future to complete. (This article is a comparative feature so I don't go into it, you can learn more about Future here )

As seen Scala adheres to a set of rules, easily converted. This allows us to focus on the application's logic implementation.

3. Immutability

Immutability in the programming language is an approach that makes everything different in the software development experience.

Immutability is the model that the object after being created cannot change its state. If you want to change the state of the invariant object, you need to create its copy and change the required properties.

In Java, the simplest example of immutability is the String object. Every time the String object changes, a copy of it is created.

Immutability can be considered the key of functional programming language (functional programming). Because it is suitable for the purpose of reducing the load of objects in the program, it helps us to easily deduce and manage the components in the program.

4. Traits in Scala and Virtual extension method in Java

Scala provides a great mechanism to extend classes. Technically, Trait provides an interface, including options (method or attribute). A class can call multiple Trait.

Note that Scala is similar to Java that doesn't support multiple inheritance, a subClass can only inherit from a superClass. But with Trait it is different, a class can call multiple Traits with the keyword "with".

With Trait, you can isolate generic features in the moduls mode and recall them at any class when needed.

In addition, recently introduced Java Extension Methods (also called Default Methods).

5. Variety of data types and libraries

Unlike Virtual Extension Methods in Java 8, Scala provides implicit classes that help you define new behaviors and use them in any class with a simple import method. For example:

One of the most noticeable things about this approach is that there is a standard set of libraries that bridge the Java library set and Scala's library. Thus in Scala code we can call the method ".asJava ()" and in Java code can call ".asScala ()" which makes richer way of handling more diverse.

6. The use of design patterns (design patern)

Visiting a forum you may come across some articles like "A factory pattern in Scala", "a Command pattern in Scala". I have read an article about the use of design concepts, the author of the article is an experienced person working on both Java and Scala languages. He said that using scala designs is not really necessary as in Java. Because the function-oriented approach approach opens more solutions and then the solution depends on the logic of the programmer, so it no longer depends on the designs.

7. Minimal codebase.

Minimum code means. This is a feature in Scala that Java has not yet adopted.

-The most typical example is declaring a class. In Java how will you reveal ???

Here's how you declare it similarly in Scala:

-Delete a variable in Java:

then in Scala you just need to declare briefly:

-How to use Java string:

then Scala provides powerful interpolation features:

In Scala also offers a multi-line string feature, which allows writing a string on multiple lines, which is really useful, such as the case of writing SQL queries:

8. Code and runtime compatibility

Scala and Java both run on the environment (jdk, jre). So when the JRE, or JDK updates Scala, will not miss any improvements from Java.

================================================== Part 2 of the article discusses some of the differences between Scala and Java, please stop here. You can follow Part 1 at this link .

Thank you for following the article.

ITZone via viblo

Share the news now