Advantages and disadvantages of Kotlin compared to Java

brief introduction

Kotlin has developed rapidly recently, making many Java developers turn to Kotlin. How do the two languages ​​compare?

Advantages of Kotlin

1. Simplify handling of null objects (prevent null pointers)

val userName = a?.b?.c?.d ?: ""

2. Attribute access, instead of Get/Set method

  • Kotlin code
class User {
    var name :String?=null
}

  • java code
public class User {
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

See why the Kotlin property access method is better than Java's Get/Set method to learn more

3. Default constructor

  • Kotlin code
class User(var name: String)

  • java code
public class User {

    private String name;

    public User(String name) {
        this.name = name;
    }

    public String getName() {
        return name.toUpperCase();
    }

    public void setName(String name) {
        this.name = name.toUpperCase();
    }
}

4. Simplify object property calls (using with, apply, let, etc.)

  • Kotlin code (to avoid repeating the name of the object)
val user = User()
with(user)
{
    name = "jerry"
    age = 18
}

  • java code
User user = new User();
user.setName("jerry");
user.setAge(18);

5. Android automatically binds the control definition in the xml file

It is no longer necessary to use findViewById or ButterKnife, use the sample:

import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        .....
        textView.text = "hello"
    }
}

6. Simplify Parcelable implementation

import android.os.Parcelable
import kotlinx.android.parcel.Parcelize
@Parcelize
class User(var age: Int,var name: String): Parcelable

7. Simplify singleton object construction

  • Kotlin code (you can use object when defining the class name)
object User
{
    fun test()
    {
    }
}

  • java code
public final class User {
    public static final User instance = new User();

    public void test() {
    }

    public static User getInstance() {
        return instance;
    }

    private User() {
    }
}

8. Use string templates to simplify string concatenation

  • Kotlin code
val info = "name:$name,age:$age"

  • java code
String info = "name:" + name + ",age:" + age;

9. Use when for branch judgment (replacement of switch in Java)

  • Kotlin code
var id = 1
when (id) {
    1 -> print("x == 1")
    2 -> print("x == 2")
    else -> {
        print("id 不是 1 ,也不是 2")
    }
}

In addition, kotlin also supports writing conditions of the same class together, such as2,3 -> print("x == 2或者3")

  • java code
int x = 1;
switch (x) {
    case 1:
        print("x==1");
        break;
    case 2:
        print("x==2");
        break;
    default:
        print("id 不是 1 ,也不是 2");
        break;
}

10. Destructuring Objects ( Destructuring Declarations ) (Java does not have this advanced gameplay)

data class User(val username: String, val age: Int)
fun main() {
    val user = User("Jerry", 18)
    val (username, age) = user
    println("username:$username,age:$age")
}

11. Coroutines

The main advantage of the coroutine is not to reduce the code, but to simplify the calling logic, so that we don't have to use complex solutions such as AsyncTask or RxJava.

GlobalScope.launch {
    doSomething()
    withContext(Dispatchers.Main)
    {
        textView.text = "coroutines完成执行"
        Toast.makeText(this@MainActivity, "coroutines hooray", Toast.LENGTH_SHORT).show()
    }
}

Finally, if you have any good learning methods or suggestions, please leave a positive message in the comments. I hope everyone can learn, work together, and make progress together.

The editor is here to wish you all the promotion and salary increase in the days to come, becoming the general manager, becoming the CEO, marrying Bai Fumei, and reaching the pinnacle of life! !

No matter what difficulties we encounter, it should not be a reason for us to give up!

Many people always encounter some problems when they first come into contact with this industry or when they encounter a bottleneck period. Follow my homepage for the sorted out learning materials or click to scan the QR code below to get it for free~

Here is about my own Android-related learning, interview documents, and video collection . Interested partners can take a look~

If you read this and think the article is well written, give it a thumbs up? If you think there is something worth improving, please leave me a message, and I will definitely check it carefully and correct the shortcomings, thank you.

Guess you like

Origin blog.csdn.net/m0_56255097/article/details/130117695