After Java, other programming languages have emerged on the JVM platform, with Scala and Kotlin among the best. Scala has become the star of the big data space, and Kotlin has emerged as a potentially powerful official support language for android following Google IO 2017. Both have been touted as “better Java,” but they use two different design philosophies.

In this article, we will look at Kotlin’s design philosophy by comparing the development paths of Java, Scala, and Kotlin.

The development of Java

It has to be said that Java is one of today’s most successful programming languages. Since its introduction in 1996, Java has dominated a large part of the programming language ecosystem. Its advantages are mainly reflected in:

  • Multiple platforms and strong community support. Java is one of the most popular programming languages in both Web development and mobile devices.
  • Respect standards, strict language specifications and backward compatibility. Java is therefore ideal for collaboration between development teams, and even if the organization changes, new people can quickly move forward with projects under the same specifications.

However, with the rapid development of computing platforms, platforms and businesses themselves present greater challenges to programming languages. The evolution of Java has also been influenced by changes in the environment. For example, with the advent of multi-core era and big data, the old functional programming has become “fashionable” again, and multi-paradigm programming languages such as Scala and Clojure begin to attract more and more developers’ attention and love. On the other hand, Java’s strict specifications are often derided as boring.

So Java has to start changing.

Java 8 exploration

If the introduction of generics in Java 5 is a significant step forward in the history of Java, so is the release of Java 8, a new exploration of Java’s future. Java 8 introduces many new language features, such as:

  • Higher-order functions and Lambda. For the first time, it broke through the design of only classes as first-class citizens, and supported functions as parameters to be passed. At the same time, combined with Lambda syntax, it changed the existing programming model;
  • The Stream API. The introduction of flows simplifies collection operations in daily development, and gives more powerful business expression and improves the readability of code;
  • Optional classes. This provides a solution at the type level to eliminate nullPointerexceptions caused by null references. This release generated an unusual response in the Java community, as Java programmers began to experience a different programming paradigm — so-called functional programming. Embracing functions also points the way in a good direction for Java.

What will Java look like in the future

At the Largest Java Summit in Europe in November 2016, Oracle’s Java Language Architect Brian Goetz shared a talk about the future of the Java language. The biggest takeaway from this conference was the exploration of language features that Java might support in the future, including:

  • Data classes
  • Value classes
  • Generic specialization
  • More powerful type inference
  • Pattern matching

The above language features, for the first taste of functional programming sweet Java developers, is very worth looking forward to. They can further liberate Java and make development more efficient and flexible. For example, once Java supports data classes, we can use a very short syntax to represent a common data object class, like this:

public class User(String firstName, String lastName, DateTime birthday)
Copy the code

With today’s Javabeans, that means many times more code, all of which can’t wait. At the same time, there may have been Java programmers working on another language on the JVM platform. The language already supports all of these new features and integrates both object-oriented and functional features from the very beginning of its design: Scala.

Scala’s treasure chest

Scala is a language created by Professor Martin Odersky of the Ecole Polytechnique Federale de Lausanne. He is also involved in the development of the Java language, with the introduction of generics in Java 5 being his masterpiece. In fact, Professor Martin began his work on Improving Java almost as soon as it was released — he explored functional programming on the JVM and released a language called Pizza that supported generics, higher-order functions, and pattern matching.

However, as he explored it later, he came to realize that Java was a language with rigid constraints that sometimes didn’t allow optimal implementation of design solutions. So he and his research partners decided to reinvent a language that would be both academically sound and practical, and that’s where Scala came in.

Academic and industrial balance

Scala is A very powerful programming language, and as its name means, Scalable, programming in Scala is like having Doraemon’s pocket full of programming language features such as object-oriented, functional, and macros.

Scala not only makes a lot of improvements in object orientation, but it also embraces the functional completely. As a result, Scala has also attracted some of the best programmers in the functional programming community, who have injected the idea of functional programming into the Scala community, thus elevating functional programming in Scala to a new level.

Due to the academic background of Scala’s designers and some of its seemingly “unusual” syntax, it was often described as “too academic” in its early days (and even now), to the point where Professor Martin, speaking at a Scala conference, quips that “What Scala really does is lead people to Haskell.”

The real Scala, however, is constantly trying to find a balance between academic and practical value. There is no denying that:

  • Scala has become a popular language in the field of big data. The star project Spark is developed in Scala, and there are many other well-known projects such as Akka and Kafka.
  • More and more commercial companies like Twitter, PayPal and Salesforce are using the language in large numbers.

Scala, on the other hand, is a language with a steep learning curve because it is powerful and flexible, and as Professor Martin himself puts it, Scala trusts the ingenuity of programmers and allows them to choose language features flexibly. But the academic and industrial balance has always been a challenge, and Scala’s multiple choices have often made it ridiculously complex compared to Java’s strict standards.

Complex but not complex

So, is Scala really complex? How many times have we heard such complaints? Before we can figure that out, we need to figure out what “complex” is. In English, the word complex is associated with two words — complex and complicated. They actually mean very different things, and a better translation for complex is “complex.” Nicolas Perony gave a Ted talk on the theory of complexity.

What is complexity? Compounding is not complicated. A complex thing is made up of many small parts, each of which is different, and each of which has its own precise role in the system. A complex system, by contrast, is made up of many similar parts, and their interactions result in a macroscopically consistent behavior. A composite system consists of many interacting elements that act according to simple, individual rules, thus leading to the emergence of new features.

Professor Martin also published an article called “Simple or Complex” that made a similar point. If you think about building blocks, Mobworld offers fixed solutions, while Lego offers endless choices. However, the number of component types of the former is much higher than that of the latter. In the same way that a programming language can build a syntax by adding features, it can also develop language features through a simple and complete theory. Scala, in Martin’s view, clearly belongs in the latter category. It is not complex, but very simple.

Simple but not easy

In fact, the most obvious feature of functional programming is its complexity. The most important thing functional development does is compose the things that need to be handled. If the object is induction, focus on the extraction and generalization of the characteristics of things; The idea of combination in functions is more like a deductive method, approximating the derivation in mathematics.

However, the philosophy of “simplicity” comes with its own costs:

  • This is a more abstract programming paradigm, where advanced functional features such as higher-order types and Typeclass provide incredibly powerful abstraction capabilities, but are more costly to learn.
  • Established a different mindset from the adoption of Java object-oriented programming. This huge difference in thinking is clearly a high bar, and one of the things that makes Scala so daunting.

Scala’s choice to embrace the functional completely also means that it’s not an easy language to become as mainstream a programming language as Java. In fact, even though many people are developing in Scala, they are still programming with a Java-like mindset. In other words, Scala is still being used as better Java, but this is the biggest demand in mainstream programming today.

In this context, Kotlin, as an emerging programming language on the JVM platform, quietly opens an equally wide door.

Kotlin – Improved Java

JetBrains started the idea for Kotlin in 2010. JetBrains is a well-known brand, and IntelliJ IDEA is one of their products. JetBrains, with their experience building development tools for a variety of languages, are naturally the most familiar people in the field of programming language design. At that time, on the one hand they saw C# in. On the other hand, Java lagged behind the new language to some extent, which made them realize the need to improve Java as a mainstream language.

The first problem the JetBrain team faced in designing Kotlin was to be compatible with the millions of lines of Java code they had, which seemed to represent one of Kotlin’s missions with the entire Java community: to be fully compatible with existing Java code. This background underlies Kotlin’s core goal of providing a better programming language for Java programmers.

Kotlin’s pragmatism

Kotlin is often considered a very Scala-like language. Indeed, both were born out of improvements in the Java language, and both built a multi-paradigm bridge between object orientation and function. There’s no denying that Kotlin has borrowed a lot from Scala, and even the creative team said, “If you’re happy with Scala, you don’t need Kotlin.”

However, Kotlin’s design philosophy is very different from Scala’s. Kotlin isn’t as interested in exploring the programming language itself as Scala is. Instead, it liberates Java while being fairly restrained in its choice of language features.

As we said, Scala aims to be a programmer’s dream language with all the language features you could ever wish for. Kotlin, on the other hand, is more grounded in reality. It still doesn’t have macros and rejects many of the so-called high-level functional language features. But it’s in Java development on the basis of a lot of language features to improve productivity, such as data, when expression pattern matching (partly), extended function (and attributes), nullable types and so on, and it seems to prefer syntactic sugar, such as Smart Casts, because this can let programmers in the engineering development easier.

As you can see, Kotlin has a very clear vision for itself — its goal is to be a useful and efficient programming language in the application world. If Scala is designed to be more than Java, Then Kotlin is really better Java.

Better Java

If you’ve done business with Kotlin, you’ll quickly realize that it’s much cleaner and more efficient than Java’s syntax. For example, Kotlin made these improvements:

  • Type derivation is largely implemented, whereas Java only supported local variable derivation in SE 10.
  • The static keyword has been dropped, but object has been introduced, which we can use directly to declare a singleton that, for comparison, Java must rely on building so-called “singleton patterns” to express equivalently.
  • By introducing “special Classes” that aren’t available in Java, such as Data Classes and Sealed Classes, we can build deeper algebraic Data types that can be used in conjunction with when expressions.

But you might ask whether Kotlin’s features, which Scala also has, are a subset of Kotlin’s. This statement is actually inappropriate. In fact, Kotlin’s efforts to become a better Java go further than Scala’s in terms of design compatibility with Java, not just with these new language features.

First of all, Kotlin is following the Java precedent closely in terms of language naming, which takes island names.

Java derives its name from the English name of The Indonesian island of Java, while Kotlin is an island near St. Petersburg, Russia.

Second, while both are Java compatible, Scala (recent versions) must require Java 8, while Kotlin can work with Java 6, which is one reason why the latter is more popular on Android.

In addition, Kotlin doesn’t explore syntax as freely as Scala does, and Java programmers can learn Kotlin’s new syntax features while retaining more of their old habits. For example, in Scala, everything has a type. So most of the time, we define a Scala function with an equal sign. The value of the last expression in the function body is the return type of the function.

def foo(x: Int) = {
    val y = x + 2
    x + y
}
Copy the code

True, Scala does away with the return keyword. In Kotlin, it also introduced the syntax of using single-line expressions to define functions, without the need for a return to return the result value.

fun foo(x: Int) = x * 2 + 2
Copy the code

In most cases, however, we can define a function in a Java-like way, such as:

fun foo(x: Int): Int {
    val y = x * 2
    return y + 2
}
Copy the code

Because Kotlin is more compatible with Java’s ecology and syntax than Scala, it is easier for Java programmers to master. Kotlin, on the other hand, pays great attention to grammatical conciseness. If you’re familiar with Implicit in Scala, the syntax of Scala is probably a bit of a shock because it’s very powerful. However, the flip side of “simple and flexible”, as we mentioned, implies abstraction and obscurity. Kotlin focused on engineering utility, so it created an extended syntax that, while less functional than Implicit, is more intuitive and still powerful enough to meet most of the needs of everyday development. Notably, Android relies on this feature, which Java does not have, to launch the extension library Android-KTX.

In addition, Kotlin has added some syntactic sugar not found in Java or Scala. If you’re working on Android, you’ve probably written Java code like this in your project:

if(parentView instanceof ViewGroup){
    ((ViewGroup) parentView).addView(childView);
}
Copy the code

We have to write ViewGroup twice for type-safety, whereas in Kotlin we can just write:

if(parentView is ViewGroup){
    parentView.addView(childView)
}
Copy the code

This relies on the Smart Casts feature in Kotlin. We don’t judge whether syntactic sugar is good or bad, but it can improve our development experience in engineering to some extent.

Overall, Kotlin aims to be a better programming language for Java productivity, with concise expressability, powerful tool support, and still very fast compilation capabilities. Development in Scala, by contrast, is often plagued by slow compilation.

Strong ecology

Now, we’ve looked at Kotlin’s overall design philosophy and its appeal over Java and Scala. Of course, there doesn’t seem to be any syntax details in this article, so don’t worry, we’ll delve into Kotlin’s language features and explore its specific advanced applications in future installments.

One more question about Kotlin is what we can do with it. Chances are you heard about Kotlin because of the news that it became an official Android language. In fact, Kotlin not only supports Android, it’s also a universal language, which can be summed up in one sentence: “Targeting JVM/JavaScript and Native”. The least we can do with Kotlin at this stage is:

  • Android development

Not only can you use Kotlin to call existing Java libraries, but you can also use the Google Kotlin extension library. Kotlin’s syntax lends itself well to Android engineering development, such as the Smart Casts we mentioned, and can also improve syntax calls to findViewById;

  • Server-side development

This is one of the biggest applications of the JVM language, and it’s certainly where Kotlin comes in. With Android support for Kotlin, Spring Framework 5 has embraced it. Based on Kotlin’s more natural functional nature, Web development with Spring is in some ways a better experience than Java;

  • The front-end development

Kotlin also has two powerful features – the Dynamic type, which allows it to interact with JavaScript, and the type-safe builder, which helps developers build reliable HTML pages. You can try using Kotlin to build the UI.

  • Native environment development

With the Kotlin Native project, Kotlin finally said goodbye to Java, leaving the JVM and compiling directly into machine code for the system environment to run. While Kotlin Native is still in its early stages, it’s worth looking forward to further developments. If you have a raspberry pie at home, try it.

As you can see, Kotlin is also a very open programming language with a strong ecology. If compatibility with Java allows it to run everywhere Java is supported, its revolutionary innovations take it beyond Java into the wider world.

summary

We’re going to end with a metaphor. Lutz Huhnken’s blog describes Java, Scala, and Kotlin as different types of skiing.

If the JVM platform was a ski world, the original Java language was the most familiar way to ski — with one ski on each foot. Scala is more like skiing with both feet on a single board. The advanced snowboarders are very enviable because they can get faster with more graceful poses, and most importantly they can do “deep powder snow” skiing, which is called functional programming.

However, for snowboarders who are used to snowboarding, trying snowboarding is like learning a new sport, with lots of falls. In fact, most people prefer to ski on two skis. At this time, the carving board appeared, using it, athletes can completely retain the original double board habit, but still can do a certain degree of deep powder snow skiing. You guessed it, it’s Kotlin.

For skiing, let’s not forget that there is a worldwide event, Android development, which is not open to snowboarding, but is open to skateboarding.

So if you’re looking for a better Java language, welcome to The ski world of Kotlin!

This is from Kotlin Core Programming