Why learn kotlin
Now that the proportion of kotlin used in android development is getting higher and higher, coupled with the grievances between google and oracle, it is foreseeable that kotlin will become the mainstream language for android development in the future.
At the same time, as an excellent high-level language, kotlin has many features that are in common with other languages, including groovy, swift, dart, etc. After learning kotlin, you will have a feeling of knowing a little bit before learning other languages.
development toolsidea
why idea instead of android studio
idea can choose to create a java project, which can test kotlin code features very quickly, and the time-consuming of a single compilation is almost negligible.
If you test kotlin in the android project, it is no problem, but it takes a long time to wait for the project to compile each time.
View kotlin's bytecode
Idea has a built-in plugin to view the bytecode compiled by kotlin, double-click shift and enter kotlin bytecode
Since bytecode is generally difficult to grasp quickly, we can choose to decompile to generate java code to help understand some implementations of kotlin features.
THIS IS IMPORTANT!!! THIS IS IMPORTANT!!!
Version selection
Choose idea community edition for free,
If it is a mac, pay attention to select the corresponding chip type
project
- file - new - create a new java project
- kotlin/jvm
script selection
- Create a new kotlin file, mainly do not select class
- Feel free to run and test kotlin features
fun main(){
println("hello world")
}
复制代码
Script debugging
In the process of testing kotlin features, we will create multiple kotlin files. If we need to run different kotlin files, we need to do two things
- Declare the main method in the file, that is
fun main(){}
, it is actually converted into a java entry function class, so that the compiler can recognize that it is a kotlin file that can be executed separately - Select edit configuration in the run configuration, and enter the name of your newly created file in the pop-up configuration window. For example, if the file name is test.kt, enter uppercase TestKt to debug the code in the test.kt script.