Today started learning Kotlin, should be completed two to three months Kolin-based learning.
Android Studio environment to build
IDEA is a first-class support Kotlin compiler, but Android programmers still prefer to use Android Studio to write demo.
Android Studio3.0
Built above the Kotlin native support, so after learning environments are compiled on the Android Studio.
When AndroidStudio create a project, use the following options:
After creating the project, because the non-domestic jcent download kotlin package will be very slow, there may be Could not download kotlin-gradle-plugin.jar
wrong, so in build.gradle will replace jecent:
Then, be built a good environment . Point hammer + run:
Initial Kotlin language
Kotlin is now a trend in the development of Android, the proportion of use of its use in Android is growing, although it is still more than 28 Java and open (open 19 years ago), but since it is a trend that we have need to learn and add to the arsenal, just as AndroidX.
Why Kotlin will become a trend, then it certainly has many of the advantages of Java less, here I list a few of its features:
- Java and JVM and interoperability that seamlessly using a variety of Java Api framework library
- Multi-platform: for Android, JS, local system programming (Native)
- The syntax is simple (that is relatively easy to learn)
- Supports type inference (Java in write int / float, Kotlin write only with val, the compiler will help you infer type)
- Distinguish between empty and non-empty type type (which is very friendly for a sentence often forget to empty Java programmers = =)
- Integrated file I / O, a regular match, threading tools
Anko库
Making development faster and Android
Overall can be summarized as:
- practical
- Minimalism
- Multi-paradigm
- Scalable
Kotlin function can be directly extended attribute. (Remember the Decorator pattern it) - Higher-order functions and closures
- DSL supports fast implementation
- Space Security
Kotlin and Java interoperability
Piece of code is given below:
//fun是函数关键字,后面跟的是函数名, 冒号后面是 返回类型
fun getList() : List<String> {
//直接使用了 ArrayList这个Java的Api
val arrayList = ArrayList<String>()
arrayList.add("A")
arrayList.add("B")
arrayList.add("C")
return arrayList
}
...
println(getList())
Print Results:
I/System.out: [A, B, C]
As can be seen from the above
- Kotlin can directly use the Java Api
- Kotlin statements do not write a comma
- Use
val
declares an ArrayList of objects
Spread function with extended attributes
Can without modifying the original class of conditions defined functions and properties so that their performance is like belonging to this class, like
for example, we have to extend a string String type the first letter of the return getFirstChar()
of function:
//直接String后面加个.然后 写出该函数
fun String.getFristChar(): String {
//this表示调用者对象
if (this.isEmpty()) {
return ""
}
//返回下标为0的字符并转成String类型
return this[0].toString()
}
...
Log.d(TAG, "abc".getFristChar())
Print Results:
D/MainActivity: a
Non-empty type with an empty safe
Koltlin introduced non-empty type with a null type to explicitly declare a variable may be null if
both compile-time type checking whether matching null pointer exception, greatly reducing the probability of a null pointer exception occurred through.
Meanwhile Kotlin also provided Elvis操作符
, 安全调用符
such as minimalist syntax sentenced to empty
For example, the following code:
When we declare a string, the system will default to infer a non-empty String, so give us a later assignment is null, the compiler will direct error
If this time we want to declare an empty String type, you write:
//声明一个可空的String类型 [名称]:[类型]?
var b:String? = "aaa"
After declaring an empty type, some methods can not be invoked, for example:
You can not use String methods, must call (?.)
or (!!.)
can, it is this:
var b: String? = "abc"
println(b?.length)
b = null
print(b?.length)
The output is:
3
null
First support functions
The same function can be passed as the value
of this feature is too top, after speaking slowly.