From Java to Scala, then Kotlin

After Java, there are some other programming languages ​​on the JVM platform, Scala, and Kotlin can be regarded as one of the best. Scala has become a star in the field of big data, Kotlin after Google IO 2017 General Assembly, seems to have become a huge potential Android platform officially supported languages. They have been dubbed a "better Java" and commendable, but they use two different design concepts.

In this paper we will by comparing Java, Scala, Kotlin these three languages ​​their own development path, to understand Kotlin design philosophy.

Java development

Have to say, Java is one of the most successful programming languages. Since 1996 Java available, it is always occupying the programming language ecology great piece. Its main advantage:

  • Multi-platform and strong community support . Whether in the development of Web or mobile devices, Java is one of the most mainstream programming languages;
  • Respect for standards, has a strict language specification and backwards compatibility . So Java is ideal for collaboration between development teams, even if organizational changes, new people can also quickly advance the project under the same specification.

However, with the rapid development of computing platform, the platform and the business itself presents a greater challenge to the programming language. Java development environment is also affected by the changes brought about. Such as the arrival of multi-core era of big data, making the old functional programming again become "fashionable", Scala, Clojure this multi-paradigm programming language began to receive attention and love more and more developers. On the other hand, Java is also strictly regulate Tucao often tedious.

Therefore, Java must begin to change.

Exploring Java 8

If Java 5 introduced generics is a major step forward in the history of the development of Java, so Java 8 release is also profound, it is its future Java development a new exploration. Java 8 introduces many new language features, such as:

  • Higher-order functions and the Lambda . The first time exceeded only class as a first class citizen of the design, support will function as a parameter to be passed, combined with Lambda syntax, changing the existing programming mode;
  • API Stream . The introduction flow simplifies the development of a set of daily operations, and given a more powerful business skills and enhance the readability of the code;
  • Optional class . NullPointerException references to eliminate problems caused by null provides a solution ideas in the type level. This time of release in the Java community attracted an unusual response, because Java programmers began to feel another programming paradigm brings new experience - the so-called functional programming. Functional guidelines also embrace a good direction for the development of Java.

Java in the future would look like

November 2016 on the summit of Europe's largest Java, Oracle's Java Language Architect -Brian Goetz shared a lecture on Java the language of the future. Greatest achievement of this meeting is to explore the future of Java language features may be supported. They include:

  • Data Class
  • Value Class
  • Generic specialization
  • More powerful type inference
  • Pattern Matching

More language features for Java developers have begun to taste the sweetness of functional programming, it is well worth the wait. They can be further emancipate Java, allows developers to work more efficient and flexible. For example, once the data support the Java class, we can use a very brief syntax to represent a common data object classes, like this:

public class User(String firstName, String lastName, DateTime birthday)
复制代码

And with today's JavaBean, it means that the amount of code several times, all people can not wait. At the same time, perhaps already Java programmers began to study another language on the JVM platform. This language has to support all these new features, and at the beginning of the design, it integrates object-oriented and functional characteristics of the two, it is the Scala.

Scala's treasure chest

Scala is a professor Martin (Martin Odersky) EPFL creation of a language. He also participated in the development of the Java language studies, Java 5 introduced generics is his masterpiece. In fact, at the time just released Java, Java Professor Martin began improvement work - he explores functional programming on the JVM platform, released a language called Pizza and then to support generics, and higher-order functions match mode.

However, in the subsequent process of exploration, he gradually found Java is a language with a rigid constraint, at some point can not be used in an optimal way to implement the design. Therefore, he and his partner decided to re-create a language that is both reasonable academically, but also have practical value, which is the origin of Scala.

Balance of academic and industrial

Scala is a very powerful programming language, as its name (Scalable- scalable) itself, with Scala programming like having A Dream pockets, filled with a variety of programming language features, such as object-oriented, function style, macro.

Scala not only a lot of improvements in object-oriented terms, and completely embraced functional. Therefore Scala also attracted a lot of programmers powerful functional programming community, they will be functional programming ideas injected into the Scala community, so will be using Scala functional programming raised to new heights.

Since Scala designer academic background, and it looks like some "unusual" syntax, which in the early development (even now) is often described as "too academic", so that a particular professor Martin Scala in the General Assembly speech, self-deprecating "Scala real role is to lead people to the Haskell".

However, the real Scala is exploring the balance both academic and practical value constantly. It is undeniable:

  • Scala has become a popular language in the field of big data, the star Spark project is to use Scala development, there are many other well-known projects such as Akka, Kafka;
  • More and more commercial companies such as Twitter, PayPal, Salesforce in the extensive use of the language.

On the other hand, Scala also has a really steep learning curve of the language, because it is powerful and flexible, as Professor Martin himself said, Scala programmers believe that intelligence, developers can use it to select the language features flexible . But the balance of academic and industry has always been a problem, compared with Java strict standards, Scala multiple choice often makes it complicated Tucao.

Complex but not complicated

So, Scala really complicated? I do not know how many times we heard complaints like this. Prior to thoroughly understand the problem, we need to be clear in the end what is "complicated." In English, the word complex can think of two words -complex and complicated. In fact, they mean very different, more precisely, complex better translation is "a composite character." Nicolas Perony has published a speech on "complex theory," in Ted.

What is the complex nature? Complex is not complicated. A complex thing is made up of many small portion composed of each part is different, and each part has its own precise role in this system. In contrast, a complex system, made up of many similar parts of the composition, but (because) the interaction between them, before the formation of A Macroscopic consistent behavior. The system contains a lot of complex interaction with the elements, they are based on simple rules of individual action, and so lead to the emergence of new features.

Professor Martin is also published an article entitled "Simple or complex", expressed a similar view. If you think of the building blocks of this matter, Mount provides a fixed program than the world, and Lego offers endless choices. However, the number of kinds of parts of the former than the latter little more of. Similar reasoning, the programming language can rely on to build the so-called cumulative function syntax is also possible to develop a simple language features complete theory, Professor Martin seems, Scala is clearly the latter, it is not complicated, but very simple.

Simple but not easy

Indeed, functional programming most obvious feature is provided of the composite. Functional development to do most things, the things that need to be addressed are combined. If the object is induction-oriented, focused on the extraction of features and summarize things; the functional composition thought more like deduction, the approximate expression is derived.

However, the "simple" philosophy also brought a corresponding price:

  • This is a more abstract programming paradigms, such as higher-order type, Typeclass and other advanced functional characteristics while providing a very powerful abstraction, but the higher cost of learning;
  • Establish another mode of thinking and the use of Java object-oriented programming different. A huge difference in this way of thinking is obviously a very high threshold, but also one of the reasons Scala prohibitive.

Scala choosing to embrace completely functional, but also means that it is not an easy language, it can not become a mainstream like Java as a programming language. In fact, even if a lot of people using Scala to develop, it is still using Java-like programming mode of thinking, in other words, it is still treated as Scala is better to use Java, but it really is the biggest industry in today's mainstream programming appeal.

In this context, Kotlin as a new programming language on a JVM platform, quietly opened the door to an equally broad.

Kotlin- improved Java

JetBrains started in 2010 to create the idea of ​​Kotlin. About the famous JetBrains, presumably a household name, famous IntelliJ IDEA is one of their products. We have the experience to build development tools for a variety of languages ​​JetBrains, naturally programming design language most familiar group of people. At that time, on the one hand they see the shine in C # on the .NET platform, on the other hand, compared to the new Java language to some extent lag, make them aware of the need to improve the door Java this mainstream language.

The first question JetBrain Kotlin design team have to face is the need to be compatible millions of lines of Java code base they have, it seems to just one mission on behalf of the Kotlin based on the entire Java community carried on the need and existing Java code is fully compatible. The background also determines the Kotlin's core objective is to provide a better programming language for the Java programmer.

Kotlin pragmatism

Kotlin is often considered a close approximation of the Scala language. Indeed, they are derived from the improvement of the birth of the Java language, while in between object-oriented and functional established a multi-paradigm bridges. It is undeniable that, Kotlin really learn a lot from Scala body, even its creative team also expressed:. "If you use Scala's very happy, then you do not need to Kotlin"

However, Kotlin and Scala's design philosophy and is very different. Kotlin did not like Scala as keen to study and explore the programming language itself. On the contrary, it is the liberation of Java, but also behave quite restrained in the choice of language features.

We said, Scala is intended to be a programmer dream language, it contains all the language features you want to have. The Kotlin more based on reality, it is at this stage still no macro, also rejected many so-called high-level functional language features. But it developed in Java on the basis of a lot to improve the productivity of language features, such as data type, when the expression (pattern matching a certain degree), spread function (and property), type, etc. can be empty, but it seems to prefer grammar sugar, such as Smart Casts, as it allows programmers easier in engineering development.

As can be seen, Kotlin self-positioning is very clear - its goal is to become a practical and efficient programming language applications. If Scala's design philosophy is more than Java (not just Java), then Kotlin is better on a true sense of the Java (better Java).

Better Java

If you use Kotlin over business development, will soon realize that it is compared to the Java syntax, even more simple, efficient, such as Kotlin made these improvements:

  • Achieve a great extent, type inference, and Java support was the derivation of local variables in SE 10;
  • He abandoned the static keyword, but the introduction of the object, we can directly use it to declare a singleton, you must rely on a comparison of Java to build the so-called "singleton" in order to express the equivalent;
  • It introduces some not in Java "special class", such as Data Classes (data type), Sealed Classes (sealed type), we can construct algebraic data types on a deeper level, the expression when used in combination.

But you may ask, Kotlin more features, Scala also, it can be said that the former is only a subset of the latter. This statement is actually inappropriate. In fact, Kotlin committed to becoming a better road Java, not just rely on these new language features, it is compatible with the design aspects of Java, a lot of work to go further than the Scala.

First of all, the language name can be seen Kotlin in strict compliance with Java precedent, they have adopted the island's name.

The name comes from the English name of Java in Indonesia melon wow island, and an island Kotlin near St. Petersburg, Russia.

Secondly, though they are compatible with Java, Scala (several recent version) must require Java 8, and Kotlin can work with Java 6, which is the latter is more popular on the Android one of the reasons.

In addition, Kotlin and did not behave like that Scala on exploring grammar was "arbitrary", Java programmers while learning grammar Kotlin new features, can still retain more of the original habits. For example, in Scala, everything type. So most of the time, we have to define a function Scala with an equal sign. The value of the function body of the last expression is the return type of the function.

def foo(x: Int) = {
    val y = x + 2
    x + y
}
复制代码

Yes, Scala abandon the return keyword. In Kotlin, which also introduces a one-line expression syntax defined in the function, you do not need to return a return result value.

fun foo(x: Int) = x * 2 + 2
复制代码

However, in most cases, we can use Java-like way to define a function, such as:

fun foo(x: Int): Int {
    val y = x * 2
    return y + 2
}
复制代码

Since Kotlin more eco-compatible Java and grammar than Scala, Java programmers can more easily grasp it. On the other hand, Kotlin great importance to the simple expression syntax. If you know in Scala implicit, it may have been scared to the Scala syntax, because it is very powerful. However, as we mentioned the other side of the "simple and flexible", it means that the abstract and obscure. Kotlin is focused on practical projects, so it creates an extended syntax, although compared to undermine implicit in function, but even more intuitive and concrete, and still very powerful, to meet the daily needs of the vast majority of development. It is worth mentioning that, Android rely on Java does not have this characteristic, it introduced extensions android-ktx.

In addition, Kotlin also added a number of Java, there is no syntactic sugar in Scala. If you work in Android development, it will definitely not written in Java code like this works:

if(parentView instanceof ViewGroup){
    ((ViewGroup) parentView).addView(childView);
}
复制代码

For the type of security we had to write twice ViewGroup, yet we can so directly written in Kotlin in:

if(parentView is ViewGroup){
    parentView.addView(childView)
}
复制代码

It relies on the Smart Casts properties in Kotlin, we do not evaluate this syntactic sugar whether good or bad, but it can improve our development experience in the project to some extent.

Overall, Kotlin is intended to be an upgrade Java programming language better productivity, it has a simple expression, powerful tool support, while still maintaining a very fast compiler capabilities. In contrast, with the Scala compiler developers are often plagued by too slowly brought.

Strong ecosystem

Now, we already know Kotlin overall design philosophy, as well as its charm compared to Java, Scala's. Of course, this does not seem to introduce any details about grammar, do not worry, we will go into Kotlin language features introduced in subsequent sections, and explore its specific advanced applications.

About Kotlin, there is a question to be answered - what we actually can do it. A high probability that you are in the news because of Kotlin Android officially supported languages ​​and know it. In fact, Kotlin not only supports Android, it is a universal language, if one sentence to sum up that "Targeting JVM / JavaScript and Native". Kotlin this stage we can use it to do at least the following things:

  • Android Development

Not only can we invoke existing Java libraries with Kotlin, but also Kotlin extensions provided by Google. Kotlin syntax is very suitable for the development of Android engineering, such as we mentioned Smart Casts, it can also be used to improve the grammar of call findViewById;

  • Service-side development

This is the largest field of application JVM language, naturally Kotlin stage play. After Android support Kotlin, Spring Framework 5 also opened its arms. , Web development will have a better experience than Java in some respects with Spring-based Kotlin more natural functional characteristics;

  • Front-end development

Kotlin There are two powerful features -dynamic type and type-safe builder, the former to achieve its interoperability with JavaScript, which can help developers build reliable HTML page. You can try to use Kotlin to build UI.

  • Native Development Environment

Because Kotlin Native project, Kotlin finally bid farewell to Java, left the JVM, compiled directly into machine code for the operating system environment. Although Kotlin Native in its early stages, but subsequent development is worth the wait. If your home has a raspberry pie, you may wish to use it to give it a try.

As you can see, Kotlin is still a very open, with a strong ecological programming language. If compatible with Java make it run all the local support Java, then it makes it revolutionary innovation beyond Java, into the wider world.

summary

We intend to use a metaphor to the end. This vivid image of the argument from Lutz Hühnken blog, he Java, Scala, Kotlin likened skiing in different species.

If the JVM platform is a ski world, the earliest Java language is the best-known ski way - feet each riding a ski to ski. Scala is more like two feet will stand on a snowboard skiing way up the coast. Those very enviable high-level athletes snowboard, because they can use more elegant posture faster speed, but most importantly, they can do "deep powder snow" slide, which is the so-called functional programming.

However, for players used to the double plate ski and snowboard to try skiing, like learning a new sport, often wrestling. In fact, most people are still more willing to double plate ski. At this time, there has been carved skateboards, use it, players can retain the original habit of double plate, but can still do the deep powder snow on a certain degree glide. You guessed right, it is Kotlin.

For the ski sport, do not forget, there is a worldwide event -Android development, it temporarily and not open to the board, but it has been engraved skateboard opened arms.

So, if you are looking for a better Java language, Kotlin Welcome to the world of skiing!

Above excerpt from "Kotlin core programming"

Guess you like

Origin juejin.im/post/5cf091e9f265da1bbb03c0c7