Kotlin基础语法
fun main(){
var price: Int = 100;
var m = 10
val b = 20
var a = 100
a = 15
val n: Double = 1.toDouble()
val o: Double? = null
val int = 1
val long = 1234567L
val double = 13.14
val float = 13.14F
val hexadecimal = 0xAF
val binary = 0b01010101
val i = 100
val j: Long = i.toLong()
val c: Char = 'A'
val k: Int = c.toInt()
println(k)
val s = "Hello Kotlin!"
val name = "Kotlin"
print("Hello $name!")
val array = arrayOf("Java", "Kotlin")
print("Hello ${array.get(1)}!")
val y = """
当我们的字符串有复杂的格式时
原始字符串非常的方便
因为它可以做到所见即所得。 """
println(y)
val arrayInt = arrayOf(1, 2, 3)
val arrayString = arrayOf("apple", "pear")
val array2 = arrayOf("apple", "pear")
println("Size is ${array2.size}")
println("First element is ${array2[0]}")
helloFunction("Kotlin")
helloFunction(name = "Kotlin")
createUser("Tom", 30, 1, 78, 2093, 10937, 3285)
createUser(
name = "Tom",
age = 30,
gender = 1,
friendCount = 78,
feedCount = 2093,
likeCount = 10937,
commentCount = 3285
)
createUser2(
name = "Tom",
age = 30,
commentCount = 3285
)
val i1 = 1
if (i1 > 0) {
println("Big")
} else {
println("Small")
}
val message = if (i1 > 0) "Big" else "Small"
println(message)
val i2: Int = 1
when(i2) {
1 -> print("一")
2 -> print("二")
else -> print("i 不是一也不是二")
}
val i3: Int = 1
val message2 = when(i3) {
1 -> "一"
2 -> "二"
else -> "i 不是一也不是二"
}
print(message2)
val array1 = arrayOf(1, 2, 3)
for (i in array1) {
println(i)
}
val oneToThree = 1..3
for (i in oneToThree) {
println(i)
}
for (i in 6 downTo 0 step 2) {
println(i)
}
}
fun helloFunction(name: String): String {
return "Hello $name !"
}
fun createUser(
name: String,
age: Int,
gender: Int,
friendCount: Int,
feedCount: Int,
likeCount: Long,
commentCount: Int
) {
}
fun createUser2(
name: String,
age: Int,
gender: Int = 1,
friendCount: Int = 0,
feedCount: Int = 0,
likeCount: Long = 0L,
commentCount: Int = 0
) {
}
fun getLength(text: String?): Int {
return if (text != null) text.length else 0
}
fun getLength2(text: String?): Int {
return text?.length ?: 0
}
|