View all my open source projects "open Source Labs"
Welcome to join my QQ group: "257053751", this blog client download "please click"
Summary
If you really want to learn more about the Kotlin language, you can view the English version of the Kotlin language document, or my Kotlin language document Chinese translation Project KOTLINDOC-CN:HTTPS://GITHUB.COM/KYMJS/KOTLINDOC-CN
This article original, reprint please specify address: http://blog.kymjs.com/
Body
Before you read the following, you may need to understand some of the features of the Kotlin language: At the end of a statement, you can declare a method with the Fun keyword, and if the function is a method that overloads the parent class, you must also add the Override keyword. The parameter of the method is the first-write parameter name followed by the colon-and-write parameter type; The Kotlin source file does not need to match the directory and package, and the source file can be placed in any file directory. But when we write classes such as Android activity, the declaration in the manifest file must match the actual package path.
Defining function methods
Example 1: The method contains two int parameters and returns an int type value
sum(a: Int, b: Int): Int { returna + b}
Example 2: The method body has only one statement and automatically guesses the return type
sum(aa + b
Example 3: If the method is public, you must explicitly write out the return type
publicIntIntInt = a + b
Example 4: Return a meaningless value (similar to void in Java)
fun printSum(a: Int, b: Int): Unit { print(a + b)}// 如果是返回Unit类型,则可以省略(对于public方法也是这样):public fun printSum(a: Int, b: Int) { print(a + b)}
Use nullable value and null value detection
The reference or function return value must be explicitly labeled nullable if it is possible to be a null value.
(A question mark after the type indicates that the object may be empty, with two exclamation points indicating that the type must not be empty)
fun main(args: Array<String>) { if2) { print("Two integers expected") return } val x = parseInt(args[0]) val y = parseInt(args[1]) //必须做判断,因为x或y有可能为空 ifnullnull) { // x 和 y 在已经检测不为null时,系统会自动将其转换为非空类型 check print(x * y) } }/** * 如果str不能转为Int类型,则返回null */fun parseInt(str: String): Int? { // (代码略)}
In keyword usage
If a number is within a range, you can use the IN keyword
//Print y-times OKif(xinch 1.. Y1)Print("OK")//If x does not exist in the array, output outif(x!)inch 0.. Array.lastindex)Print("Out")//print 1 to 5 for(xinch 1.. 5)Print(x)//Traversal collection (similar to for (String Name:names) in Java) for(NameinchNames) println (name)//If the Names collection contains a text object, print Yesif(text inchNames
Type detection and automatic conversion
is keyword usage (similar to the instanceof keyword in Java)
Example 1
fun getStringLength(obj: Any): Int? { ifis String) { // 做过类型判断以后,obj会被系统自动转换为String类型 return obj.length } //在这里还有一种方法,与Java中instanceof不同,使用!is // if (obj !is String){ // // XXX // } // 这里的obj仍然是Any类型的引用 returnnull}
Example 2
// 在左侧obj已经被判断为String类型,所以在&&的右侧可以直接将obj当成String类型来使用 if (obj is String && obj.length0) { return obj.length } returnnull}
When expression
(similar to switch in Java)
Fun Cases (obj: any) { when(obj) {1 - Print("First item")"Hello" - Print("This is the string hello") isLong - Print("This is a long type of data") ! isString - Print("This is not a string type of data")Else - Print("Else similar to default in Java") }}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Kotlin Basic Syntax