Home » Java » We got through Kotlin what?

We got through Kotlin what?

what do we get from Kotlin through


doesn't go deep into the Kotlin language (we'll go back to it in the next chapter). Here are some of the interesting features that aren't in Java:

easy to show

through Kotlin, can more easily avoid template code because most of the typical case in the language default coverage achieved. For example, in Java, if we want to be a typical data, we need to write (at least the generated code:

public, class, Artist {
Private long id;
Private String name;
Private String url;
Private String mbid;
Public, long, getId () {
Return id;
Public, void, setId (long, ID) {
This.id = id;
Public, String, getName () {
Return name;
Public, void, setName (String, name) {
This.name = name;
Public, String, getUrl () {
Return url;
Public, void, setUrl (String, URL) {
This.url = url;
Public, String, getMbid () {
Return mbid;
Public, void, setMbid (String, mbid) {
This.mbid = mbid;
@Override, public, String, toString () {
Return "Artist{" +
"Id=" + ID +
"Name='" + name +' '+
"Url='" + URL +' '+
"Mbid='" + mbid +' '+

uses Kotlin, and we just need to pass the data class:

data, class, Artist
Var id: Long,
Var name: String,
Var url: String,
VaR, mbid:, String)

this data class, which automatically generates all attributes and their accessor, and some useful methods, such as toString ()

empty security

When we use the

Java development, our code is mostly defensive. If we don't want to meet NullPointerException, we need to use it before going to determine whether it is null.Kotlin, as many modern languages, is safe, because we need to through a secure call operator (write do?) to explicitly specify whether an object can be empty.

We can write like this:

// cannot compile here. Artist cannot be null
VaR, notNullArtist:, Artist = null
/ / Artist can be null
VaR, artist:, Artist = null
/ / artist may be unable to compile, null, we need to deal with
Artist.print ()
As long as the artist / / null = will print!
Artist,.Print ()
/ / intelligent conversion. If we were empty check before, you do not need to use the call operator call security
If (artist = = null) {
Artist.print ()
Ensure that artist is not only in the / / null case can be so called, otherwise it will throw an exception
Artist!.print ()
/ / using the Elvis operator in null is given a value in the absence of alternative
Val name = artist?.name:: "empty"

extension method

, we can add functions to any class. It's more readable than the typical tool classes in our project. For example, we can add a function that displays toast to fragment:

fun Fragment.toast (message:, CharSequence, duration:, Int = Toast.LENGTH_SHORT) {
Toast.makeText (getActivity (), message, duration),.Show ()

, we can do it now:

fragment.toast ("Hello world") 

function support (Lambdas)

Every time we go to the

statement trigger a click event, you can only need to define what we need to do, rather than have to implement an internal class? We can do this (or other more interesting events) we need to thank lambda:

view.setOnClickListener {toast ("Hello world")}

here just a small part of Kotlin can simplify our code. Now that you know some interesting features of the language, you can consider whether it is appropriate for you. If you choose to continue, we will start our journey after practice >.