Introduction to JVM Languages
上QQ阅读APP看书,第一时间看更新

Java is a very verbose language

Java is notorious for being very verbose. While over the years there have been updates made to the language to improve the situation a bit, many other languages require the writing of less code for the same end result.

Let’s look at a simple example.

A standard mutable object often looks like this in Java:

    class Person {
private String name;
public Person(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}

In Kotlin, the following line of code does the same (and more):

    data class Person(val name: String)

No, this is not a joke. Kotlin automatically implements the same methods as shown in the Java example when compiling this code. In fact, it adds even more commonly used methods than shown here in the Java example. In Chapter 4, Java Programming, we will discuss those additional methods for Java as well.

While productivity can be seriously enhanced by choosing a different language, the situation for Java is not as bad as it may seem. All modern IDE programming tools can automatically generate boilerplate Java code, such as the one shown in the preceding code, with a simple key press combination.