xxi
Introduction
The programs explained in this book, despite their strong affinity
to the Kotlin way of
thinking, will not be totally mysterious to Java developers or developers of other modern
computer languages. One of the design goals of Kotlin is expressiveness,
so understanding
Kotlin programs requires little effort, even when the programs get shorter. But at some point,
you have to pay for maximum brevity with a loss of expressiveness and a loss of readability.
When it comes
to deciding what is better, I favor expressiveness over brevity, but be assured
that a loquacious programming style is a no-go.
In the end, professional developers want to
write concise apps because less code means lower costs when it comes to maintenance.
The Transition from Java to Kotlin
Just
to whet your appetite, you will now take a look at a really simple app—one that lacks
a lot of features you would want to see in a more complex and professional app—and then
rewrite it from Java to Kotlin. The app consists of a single activity and presents an edit field,
a button, and a text field that reacts to button presses.
If you want to
create it using Android Studio, you initiate a new project, disable Kotlin support,
and edit
the layout to contain a
TextView
widget, a
Button
widget, and an
EditText
widget.
Then assign the IDs
edit
,
btn
, and
text
to them, respectively. The Java code is as follows:
package de.pspaeth.simplejava;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.*;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);