Just ended the Google IO Conference really dry, as programmers most let me just interested in or Google announced that Kotlin become the first development language Android
This is indeed a more shocking news, as the mobile development of me, is bound to grasp the development of the language in time.
Personally read Kotlin's Chinese document, feel its grammar and swift very similar (in the end who copied who ~), so if you have the effect of swift development of the iOS program, mastering the language is not very difficult! Needless to say, the following is dry:
1. Constants and variables
常量:val a: Int 1val1val//当没有初始化值时必须声明 类型1//赋值变量:var51
(Swift's constant Let, VAR, variable definition is the same ~ ~)
2. Functions
suma: Int, b: Int): Int { returna + b}
Kotlin language and Swift language, the code does not need to use ";" End
The code above can actually continue to simplify
suma: Int, b: Int)suma + b
The return type of the function is determined by the variable following =
no return value when-unit: equivalent to void
fun printSum(a: Int, b: Int): Unit { a
Unit can be omitted
fun printSum(a: Int, b: Int) { a
3. Main function
Array<String{}
4. If condition statement
max(a: Int, b: Int): Int { if(a>b) returna else return b}
Simplified notation
max(aif (aaelse b
5. Use nullable variables and null values to check
Swift has a type of optional
Kotlin also has this type of operation
parseIntString): Int?{}
It is possible for the function to return an int and possibly return null
So null judgment is required for use
Array<String>) { parseInt(args[0]) parseInt(args[1]) //这里使用x、y容易出现错误,因为x和y有可能是null ifnullnull){ print(x * y) }}
Type qualifier is
if(str is String){ //判断类型 returnstr.length}elsereturnnull
Is can be type auto-conversion
fun getStringLength(obj : Any): Int? { ifis String){ //obj 将会在这个分支自动转换为String类型 return obj.lengrh } //obj 还是Any类型 returnnull}
A swifter Kotlin to learn--kotlin basic grammar