Kotlin VS Java: Basic syntactic differences __android applications

Source: Internet
Author: User

Kotlin is younger than Java, but it is a very promising programming language and its community is growing. Everyone is talking about it and saying it's cool. But why so special.
We have prepared a series of articles to share our experience in developing Android applications in Kotlin. We'll discuss the differences between Kotlin and Java in terms of syntax, usability, UI performance, and asynchrony so that you can decide which language is best for you.
Let's start with some basic grammatical differences. This is the first one:

1. Using Kotlin, you can do more with less code

One of the main advantages of Kotlin is its simplicity. 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 the class.

Public final class Person {private String name;
    private int age;

    private float height;
        Public person (String name, int age, float height) {this.name = name;
        This.age = age;
    This.height = height;
        Public person (String name, int age) {this.name = name;
        This.age = age;
    This.height = 1.8f;
    Public String GetName () {return name;
    public void SetName (String name) {this.name = name;
    public int getage () {return age;
    public void Setage (int age) {this.age = age;
    public float getheight () {return height;
    public void SetHeight (float height) {this.height = height;
                @Override public String toString () {return "person{" + "Name= '" + name + ' \ ' +
    ", age=" + Age + ", height=" + Height + '} '; } @Override Public BooLean equals (Object o) {if (this = O) return true;

        if (o = = NULL | | getclass ()!= O.getclass ()) return false;

        person who = (person) o;
        if (age!= person.age) return false;
        if (Float.compare (person.height, height)!= 0) return false; Return name!= null? Name.equals (person.name): Person.name = = null} @Override public int hashcode () {int result = Name != null?
        Name.hashcode (): 0;
        result = ~ Result + age; result = ~ Result + (height!= +0.0f?)
        Float.floattointbits (height): 0);
    return result;
 }   
}

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

Data class person (Var name:string,
var age:int,
var height:float = 1.8f)

Yes, you will automatically get the required getters,setters,equals (), Hashcode (), toString () and copy () functions for your data class. Of course, you can easily rewrite these functions, but in most cases simply declaring the class and its properties is sufficient.
That's exactly what we mean when we say Kotlin concise.

2. You can 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 the null pointer since Tony Holl invented it in 1965, while trying to make things simpler.
Sadly, we can't come back in time to prevent Tony from making this mistake. But with Kotlin, we can now easily escape nullpointerexception.

Val Person:person? = null
...
Person?. Name = "John"

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

L2
linenumber L2
aload 3 DUP ifnull L3 LDC
"John" invokevirtual
igalata/com/kotlinexample /person.setname (ljava/lang/string) V
GOTO L4
L3
POP

As you can see, we have the same empty check here. JetBrains developers (creating Kotlin) know that checking our variables every time is the only way to avoid nullpointerexception. But they also know that Android developers don't want to deal with NullPointerException in their projects. They might think: "Why not automatically generate this check if the variable is nullable."
JetBrains's developers are just like that, making our lives easier.

3. You can get rid of the Util class

Let's talk about ugly things about using the Util class. Do you have a project without them. We hardly remember all this. Kotlin has a smart solution-extended functionality-to help you get rid of all the util classes once and for all.
An extension function is almost usually a kotlin function. But when you declare it, you need to specify the instance that will have the extended functionality of the class.

Fun Context.toast (text:string) = Toast.maketext (this, text, Toast.length_short). Show ()

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

Toast ("Hi")

You should keep in mind 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 byte code again.

Public final toast (landroid/content/context; ljava/lang/string;) V
    

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.