Kotlin VS Java: Basic Syntax Differences (reprint)

Source: Internet
Author: User

May 18, Goole announced that Kotlin became the official support of the development of the language, Kotlin language community, the public, QQ group and other comprehensive bombing, this article is a translation, a user from abroad, will introduce you, basic grammar part Kotlin and Java differences, A basic comparison can be made for the developers of Android using Java.

1. Kotlin,do Less,get More

One of the main advantages of Kotlin is its brevity. You get more functionality with less code. The less code you write, the less mistakes you make. It's simple. Let's look at the basics of Kotlin, starting with classes.

The above is a common Java class. It does not have much to do. It contains only some data. But when you realize what it does to the table, it's painful to see how big the code is. To encourage you, we will give you an equivalent class written in Kotlin.

Yes, you will automatically get the required Getters,setters,e quals (), Hashcode (), toString () and copy () functions for your data classes! Of course, you can easily rewrite these functions, but in most cases it is sufficient to simply declare the class and its properties.

That's exactly what we mean when we say Kotlin concise.

2. Avoid NullPointerException

Now we want to remind you of the greatest pain in many programming languages-null pointer exceptions. We can hardly imagine how many developers have been exposed to null pointers since Tony Holl invented it in 1965, while trying to make things easier.

Sadly, we can't come back in time to prevent Tony from making this mistake. But with Kotlin, we can now easily escape nullpointerexception.

If the variable is nullable, the compiler will not allow you to access it without proper checking. Kotlin forcing you to use? Operator. This will prevent the application from automatically crashing.
How does it work under the hood? Let's review the generated bytecode.

As you can see, we have the same empty check here. JetBrains developers (create Kotlin) know that each time we check our variables is the only way to avoid nullpointerexception. But they also know that Android developers do not want to handle nullpointerexception in their projects. They might think, "Why not automatically generate this check if the variable is nullable?"

JetBrains developers just like this, make our life easier!

3. Cast off utils (Get rid of tool class)

Let's talk about the ugly things about using the Util class. Do you have a project without them? We hardly remember all this. Kotlin has a smart solution-extension feature-to help you get rid of all the util classes once and for all.

An extension function is almost always a kotlin function. But when you declare it, you need to specify that the instance will have the extended functionality of the class.

Note the ' This ', which we pass as parameters to the Maketext () method? It is not an instance of a class, we declare this function, but rather a context instance. Now you can call this function directly from your activity or any other context instance. For example:

You should remember that the extension function does not modify its extended class in any way. So how does it work without changing the original class? Let's see the bytecode again.

Ha! Your function implicitly receives an instance of the class that it extends as the first parameter. Therefore, in bytecode, any access to "this" in the function body is replaced with access to the first parameter. No magic, really. You can use this function at any point in your project.
Time to delete your Util pack!

4. You can forget the view bindings

Do you remember Findviewbyid () method ()? We believe you don't like it. Neither are we. In addition, we do not want to declare variables and butterknife annotations for each view that we need to access.

You can forget the view bindings with Kotlin Android extensions. You no longer need to create variables and bound views. You can access your views directly using identifiers declared in the XML layout.

It's too easy, isn't it?


Basically, the Findviewbyid () method is still in use. But there is no need to write it yourself. Kotlin will do it for you.

When you use the Android extension, the Findcachedviewbyid () function and the HashMap instance are automatically generated. Each time you access your view by its identifier, it will be replaced by a new function call. If you are accessing the view for the first time, this function calls the usual Findviewbyid () function and adds the received view to HashMap to retrieve the view from the next time you access the view.

5. You can make it easier to use collections

Let's talk about the collection of Kotlin. Because we often need to use the data model collection to perform difficult operations. For example, we may have a student list where we need to retrieve three grade A students and two B grade students.


Check out the Kotlin solution:

Here's how we solve the same problem in Java:

This is just a small example of how collections are used in Kotlin and Java, but you can see the difference! Can you imagine what the difference would be if we were dealing with a collection of large items, Kotlin?

Translator: Kotlin relative to the greatest advantage of Java, is concise, but java9 out, some of the tedious problems should be resolved, at the same time if the use of Kotlin to develop Android, commonly used frameworks and so on can not be used, But learning Kotlin is still necessary, Kotlin language style is more modern, grammar directly inherit ActionScript, with modern style, to solve the old problems of Java for many years, eliminating the trouble of using various kinds of ecological tools, the mainstream programming language to push forward a step, No longer cling to the grammatical form of C-era. As the Kotlin continues to improve, it will be a good Android language to match Java.

Kotlin VS Java: Basic Syntax Differences (reprint)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.