Kotlin files are suffixed with .kt.
Package statement
The beginning of the code file is generally the declaration of the package:package com.tienanhvn.mainThe kotlin source files do not need to match the directories and packages, and the source files can be placed in any file directory.
import java.util.*
fun test() {}
class Tienanhvn {}
In the above example, the full name of test() is com.runoob.main.test, and the full name of Tienanhvn is com.tienanhvn.main.Tienanhvn.
If no package is specified, the default is the default package.
Default import
Multiple packages are imported into each Kotlin file by default:Kotlin.*Function definition
Kotlin.annotation.*
Kotlin.collections.*
Kotlin.comparisons.*
Kotlin.io.*
Kotlin.ranges.*
Kotlin.sequences.*
Kotlin.text.*
The function definition uses the keyword fun, and the parameter format is: parameter : type
fun sum(a: Int, b: Int): Int {The expression is a function body, and the return type is automatically inferred:
return a + b
}
fun sum(a: Int, b: Int) = a + bA function with no return value (similar to void in Java):
public fun sum(a: Int, b: Int): Int = a + b
fun printSum(a: Int, b: Int): Unit {
print(a + b)
}
public fun printSum(a: Int, b: Int) {
print(a + b)
}
Variable length parameter function
The variable length parameter of a function can be identified by the vararg keyword:fun vars(vararg v:Int){
for(vt in v){
print(vt)
}
}
fun main(args: Array<String>) {
vars(1,2,3,4,5) // 输出12345
}
Lambda (anonymous function)
An example of a lambda expression:
fun main(args: Array<String>) {Defining constants and variables
val sumLambda: (Int, Int) -> Int = {x,y -> x+y}
println(sumLambda(1,2))
}
Variable variable definition: var keyword
Var <identifier> : <type> = <initialization value>Immutable variable definition: val keyword, a variable that can only be assigned once (similar to the variable modified in Java).
Val <identifier> : <type> = <initialization value>Constants and variables can have no initialization value, but must be initialized before reference
The compiler supports automatic type judging, that is, the type can be specified without being specified by the compiler.
val a: Int = 1Comment
val b = 1
val c: Int
c = 1
var x = 5
x += 1
Kotlin supports single-line and multi-line comments, examples are as follows:
// This is a one-line commentUnlike Java, block comments in Kotlin allow nesting.
/* This is a multi-line
Block comment. */
String template
$ represents a variable name or variable value
$varName represents the value of the variable
${varName.fun()} represents the method return value of the variable:
var a = 1NULL check mechanism
val s1 = "a is $a"
a = 2
val s2 = "${s1.replace("is", "was")}, but now is $a"
Kotlin's empty security design is for nullable parameters. When using it, it needs to be judged empty. There are two ways to deal with it. After the field is added!! Throw an empty exception like Java, and add another field. Do not do it. Processing return value is null or match?: Short judgment processing
var age: String? = "23"When a reference may be a null value, the corresponding type declaration must be explicitly marked as nullable.
val ages = age!!.toInt()
val ages1 = age?.toInt()
val ages2 = age?.toInt() ?: -1
Returns null when the string content in str is not an integer:
fun parseInt(str: String): Int? {The following example demonstrates how to use a function whose return value can be null:
// ...
}
fun main(args: Array<String>) {Type detection and automatic type conversion
if (args.size < 2) {
print("Two integers expected")
return
}
val x = parseInt(args[0])
val y = parseInt(args[1])
if (x != null && y != null) {
print(x * y)
}
}
We can use the is operator to detect if an expression is an instance of a type (similar to the instanceof keyword in Java).
fun getStringLength(obj: Any): Int? {
if (obj is String) {
return obj.length
}
// if (obj !is String){
// // XXX
// }
return null
}
fun getStringLength(obj: Any): Int? {
if (obj !is String)
return null
return obj.length
}
fun getStringLength(obj: Any): Int? {Interval
if (obj is String && obj.length > 0)
return obj.length
return null
}
The interval expression is formed by the rangeTo function with the operator form .. with in and !in.
The interval is defined for any comparable type, but for an integer native type, it has an optimized implementation. Here are some examples of using intervals:
No comments:
Post a Comment