Why do I give up Java learning Kotlin?

Source: Internet
Author: User

Kotlin Introduction

Kotlin has long been a hot topic in Android development, and more and more projects on GitHub are being developed with Kotlin. But the language has been in a very awkward position, on the one hand to explore their own role in the development of Android, while not officially supported by the language, leading to a lot of people actually hold a wait and see attitude.

Yesterday at the I/O Conference, Google announced that Kotlin officially became the official language of Android. There is a word called no picture of a infertile, to 2 HD big picture first:

Originally I was ready to watch live all night, think of their English, foreign language do not understand, or the next day to see the record broadcast, for Kotlin, I was yesterday just know, but!! I found a post in Google, in June 15, predicted that Kotlin might replace Java. So what's the difference between Kotlin and Java? How do we explain this? You can understand the difference between swift and object-c. Nearly two days, the major websites, the public number. All have been kotlin brush screen. Another wave of technical heat, in fact, in this bottom, I will not mvp,mvvn,rx,rn and so on (hot repair is going to see, not put in it). So this wave of technical heat, I will not participate in it? Well, obviously, I will. As to what Kotlin is, we're going to look at it next.

What is Kotlin?

Kotlin is from JetBrains company, named after the Kotlin Island near St. Petersburg. He can be run on the JVM virtual machine, can also be compiled into JavaScript source code, compatible with java100%. Let's look at the birth process of Kotlin:

    • 2011.6 Project Disclosure
    • 2012.2 Open Source Https://github.com/JetBrains/kotlin
    • 2013.8 Support Androidstudio
    • 2014.6 new Open source Web site and domain name https://Kotlinlang.org
    • 2015.3 Eclipse Plugin Debut
    • 2015.4 Anko Debut
    • 2016.2 1.0 Official Release

As a provider of the currently popular Java IDE IntelliJ, the Kotlin programming language has been open source under the Apache license. And it happened that I always used INTELLJ idea. I have to say I admire JetBrains. This company not only produced the most popular Java development tools IntelliJ Idea,android Studio was developed based on the IntelliJ Idea Community Edition and created a language of its own. But JetBrains is not an American company, but Prague in the Czech Republic. JetBrains's vision for Kotlin: End-to-end development of different applications bridging multiple platforms in the same language, including full stack Web applications, Android and iOS clients, embedded/Internet of things, etc.

Why do I need Kotlin?
    • NULL type safety
    • Lambda expression
    • Extension methods
    • Type deduction
    • It's even harder to do everything that Java can do.
    • No semicolon!!!!

Mom in also do not worry about my nullpointerexpection, because in general, if you project has a null pointer, is in operation after the error, and use the Kotlin, compile directly will not pass, but the lack of nullpointerexpection, Isn't coding a lot less fun? Lambda expression don't know what it is? I don't know!!! But with Kotlin you do not worry about you will not lambda, at that time I was because I do not understand so give up, but in Kotlin inside, if you do not understand, come and tell me, see I do not kill you!! Hem ~ ~ ~

Androidstudio 3.0 automatically adapt to the Kotlin, of course, now you can install plug-ins, or directly with INTELLJ, this we say, we can put all of our project's Java files directly into the Kotlin file, finally Kotlin has an advantage, No semicolon!! No semicolon!! No semicolon!! The important thing to say three times, do not worry about too many brackets, where the semicolon to add the question ~ ~ ~

Why do I give up Java learning Kotlin?

Just came out yesterday, now CSDN poll now also asked Java will not be replaced by Kotlin. I had a message before. After all, Google is dad, what he said is what. However, what I consider to be a replacement is just a substitution in Android development, not any scenario. Yesterday because there is no time, today I also briefly study the next Kotlin. The grammar should be about the same as JS. I remember the last two years when I read HTML in school, I refused to write JS and CSS. Learn Java in the look of JS is a face blind. However, in order to learn Kotlin, I spell, after the completion of the discovery is really easy.

How to configure Kotlin

Androidstudio is the need to install plug-ins, and INTELLJ do not need, because most developers use the Androidstudio, then follow my steps, step by step to:

First of all, since we are loaded plug-ins, we continue to open File->settings->plugins->install jetbrains Plugins ...

After the installation restart it will be OK, I installed this side, do not do a demonstration. Now we can happily write our Kotlin code ~ ~ ~

Hello Kotlin

HelloWorld is the first lesson in any program that the ape learns, but Hello Kotlin is better for me.

So, let's start with a new Kotlin activity file:

My side is already written, and the code you generated should look like this:

class MainActivity : AppCompatActivity() {}

Empty, we first rewrite its oncreate () method and then put the layout we set in:

class MainActivity : AppCompatActivity() {    override fun onCreate(savedInstanceState: Bundle?) {        super.onCreate(savedInstanceState)        setContentView(R.layout.main)    }}

A simple Hello Kotlin is done ~
Do not know whether or not remember my Android first lesson, this time we come to a Kotlin first lesson

Kotlin_button = Findviewbyid (R. ID. Kotlin_test) as button?//main. Kotlin_test. SetText("This is a test")//Main. Kotlin_test. Text="This is a test"Kotlin_textview = Findviewbyid (R. ID. Get_kotlin_test) as TextView? kotlin_textview!!. Text="Kotlin First Lesson"kotlin_textview!!. TextColor= Color. REDkotlin_textview!!. BackgroundColor= Color. BLUEvar SSB = Spannablestringbuilder (kotlin_textview!!. Text)//settings [0,7] The font color SSB. Setspan(Foregroundcolorspan (Color. YELLOW),0,6, spanned. SPAN_exclusive_inclusive) kotlin_textview!!. Text= SSB

The previous comment is to let you understand, for Findviewbyid and SetText in Kotlin can write so, well, say not much, we first look at the effect of the operation:

Yes, perfect~.

Hello Anko

Anko is used to rely on the Kotlin implementation, first of all, we need to do the following dependencies:

    // Anko    ‘org.jetbrains.anko:anko-sdk15:0.8.2‘// sdk19, sdk21, sdk23 are also available    ‘org.jetbrains.anko:anko-support-v4:0.8.2‘// In case you need support-v4 bindings    ‘org.jetbrains.anko:anko-appcompat-v7:0.8.2‘// For appcompat-v7 bindings

What exactly is the

Anko for? He can put a layout in the method, but I do not recommend the use of Anko, because the MVC feeling is not there, and the code back to the original era, a complex page, you write with Anko, function and layout in a method, which can play? However, we still have to write a demo run to play, the code is very simple:

 verticallayout {button {id  = R.id . Kotlin_test text  =  "I am a kt file-written layout button"  OnClick {kotlin_textview!!. text  = Kotlin_button!!. text . toString () Toast ( "Hello" ) Startactivity<secondactivity> ( "text"  to  "I am the first page to go in" )}} textView {id  = R.id . Hello_kotlin text  = " Hello Kotlin "}} 

Verticallayout is a vertical linear layout, we put a button and a textview. Let's run for a moment and look at the effect first:

Fix ~, but do not know people look carefully my code is not, I wrote a startactivity method, yes, startActivity<SecondActivity>() this is to start another activity. startActivity<SecondActivity>("text" to "我是第一个页面进去的")we can look in front of me put a text behind put a string, in the middle there is a to, this is the activity with parameters of the value of use. Let's take a look at how our secondactivity calls this obtained data:

 override fun onCreate(savedInstanceState: Bundle?) {        super.onCreate(savedInstanceState)        setContentView(R.layout.second)        val extra = intent.getStringExtra("text")        text_second.text = extra.toString()    }

is not soeasy. Let's run for a minute. First:

How to feel, to tell the truth, sometimes I feel better than annotation, a wave of 666 to send on, as for the back there are a lot of, fragment,adapter, custom view, etc., can be resolved. We can do this by:

Get you to write the method in Java to convert the Kotlin, but there is a drawback is that Kotlin can not be converted to Java, so, use with caution!!

Summarize

From this afternoon half-day my understanding of Kotlin, is easy to use, 666. If mastered, our code will be simpler and more concise. I will be in the future study, constantly interspersed with the use of Kotlin.

For individual projects to turn to Kotlin, usually not difficult to choose, after all, Kotlin is such a good language, I believe many people still willing to try and use the language of the multiplier.

The more difficult choice is how to let the team to switch to Kotlin, the individual think the team is difficult to switch for many reasons, such as learning costs, historical baggage and so on. But in fact the root cause is still the problem of the way of thinking, crooked nuts like to use tools to improve the development efficiency, because the human cost is very high. The way in which domestic teams improve efficiency is usually to increase membership.

Soon after, Android development will slowly move from Java to Kotlin. So, if there is time, still try to know a little bit about the use of Kotlin.

Why do I give up Java learning Kotlin?

Related Article

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.