logo
💻 编程语言

Kotlin

Kotlin Cheat Sheet - 快速参考指南,收录常用语法、命令与实践。

📂 分类 · 编程语言🧭 Markdown 速查🏷️ 2 个标签
#kotlin#android
向下滚动查看内容
返回全部 Cheat Sheets

Introduction to Kotlin

main()
KOTLIN
滚动查看更多
fun main() {
  println("Greetings, CheatSheets.zip!")
  // Code goes here
}

The main() function is the starting point of every Kotlin program and must be included in the code before execution

Print statement
KOTLIN
滚动查看更多
println("Greetings, earthling!")
print("Take me to ")
print("your leader.")

/*
Print:
Greetings, earthling!
Take me to your leader.
*/
Notes
KOTLIN
滚动查看更多
// this is a single line comment

/*
this
note
for
many
*/
Execution order
KOTLIN
滚动查看更多
fun main() {
  println("I will be printed first.")
  println("I will be printed second.")
  println("I will be printed third.")
}

Data Types and Variables

Mutable variables
KOTLIN
滚动查看更多
var age = 25
age = 26
Immutable variables
KOTLIN
滚动查看更多
val goldenRatio = 1.618
Type inference
KOTLIN
滚动查看更多
// The following variables are assigned a literal value inside double quotes
// so the inferred type is String

var color = "Purple"
String concatenation
KOTLIN
滚动查看更多
var streetAddress = "123 Main St."
var cityState = "Brooklyn, NY"

println(streetAddress + " " + cityState)
// Print: 123 Main St. Brooklyn, NY
String Templates
KOTLIN
滚动查看更多
var address = "123 Main St."
println("The address is $address")
// prints: The address is 123 Main St.
Built-in Properties and Functions
KOTLIN
滚动查看更多
var monument = "the Statue of Liberty"

println(monument. capitalize())
// print: The Statue of Liberty
println(monument. length)
// print: 21
Character escape
KOTLIN
滚动查看更多
print("\"Excellent!\" I cried. \"Elementary,\" said he.")

// Print: "Excellent!" I cried. "Elementary," said he.
  • \n insert new line
  • \t inserts a tab
  • \r inserts carriage return
  • \' inserts a single quote
  • \" inserts a double quote
  • \\ inserts a backslash
  • \$ inserts a dollar sign
Arithmetic Operators
KOTLIN
滚动查看更多
5 + 7  // 12
9 -2   // 7
8 *4   // 32
25 /5  // 5
31 % 2 // 1

+ addition, - subtraction, * multiplication, / division, and % modulus

Order of operations
KOTLIN
滚动查看更多
5 + 8 *2 /4 -3 // 6
3 + (4 + 4) /2 // 7
4 *2 + 1 *7    // 15
3 + 18 /2 *1   // 12
6 -3 % 2 + 2   // 7
Enhanced assignment operator
KOTLIN
滚动查看更多
var batteryPercentage = 80

// long syntax
batteryPercentage = batteryPercentage + 10

// short syntax with augmented assignment operator
batteryPercentage += 10
Increment and decrement operators
KOTLIN
滚动查看更多
var year = 2019
year++   // 2020
year--   // 2019
Math library
KOTLIN
滚动查看更多
Math.pow(2.0, 3.0) // 8.0
Math.min(6, 9)     // 6
Math.max(10, 12)   // 12
Math. round(13.7)  // 14

Conditional Expression

If expression
KOTLIN
滚动查看更多
var morning = true

if (morning) {
  println("Rise and shine!")
}
// Print: Rise and shine!
Else-expression
KOTLIN
滚动查看更多
var rained = false

if (rained) {
  println("No need to water the plants today.")
} else {
  println("The plant needs to be watered!")
}
// print: The plant needs watering!
Else-If expressions
KOTLIN
滚动查看更多
var age = 65

if (age < 18 ) {
  println("You are considered a minor")
} else if (age < 60) {
  println("You are considered an adult")
} else {
  println("You are considered senior")
}

// print: you are considered senior
Comparison Operators
KOTLIN
滚动查看更多
var myAge = 19
var sisterAge = 11
var cousinAge = 11

myAge > sisterAge  // true
myAge < cousinAge  // false
myAge >= cousinAge // true
myAge <= sisterAge // false
Logical Operators
KOTLIN
滚动查看更多
var humid = true
var raining = true
var jacket = false

println(!humid)
// print: false
println(jacket && raining)
// print: true
println(humid || raining)
// print: true
AND operator: &&
KOTLIN
滚动查看更多
var humid = true
var raining = true
var shorts = false
var sunny = false

// true AND true
println(humid && raining) // true
// true AND false
println(humid && shorts)  // false
// false AND true
println(sunny && raining) // false
// false AND false
println(shorts && sunny)  // false
Or operator:||
KOTLIN
滚动查看更多
var late = true
var skipBreakfast = true
var underslept = false
var checkEmails = false

// true OR true
println(skipBreakfast || late) // true
// true OR false
println(late || checkEmails)   // true
// false OR true
println(underslept || late)    // true
// false OR false
println(checkEmails || underslept) // false
NOT operator
KOTLIN
滚动查看更多
var hungry = true
var full = false

println(!hungry) // false
println(!full)   // true
Evaluation order
KOTLIN
滚动查看更多
!true && (false || true) // false
/*
(false || true) is evaluated first to return true.
Then, evaluate !true && true and return the final result false
*/

!false && true || false // true
/*
!false is evaluated first to return true.
Then true && true is evaluated, returning true.
then, true || evaluates to false and eventually returns true
*/
Nested conditions
KOTLIN
滚动查看更多
var studied = true
var wellRested = true

if (wellRested) {
  println("Good luck today!")
  if (studied) {
    println("You should prepare for the exam!")
  } else {
    println("Spend a few hours studying before the exam!")
  }
}

// Print: Good luck today!
// print: You should be ready for the exam!
When expression
KOTLIN
滚动查看更多
var grade = "A"

when (grade) {
  "A" -> println("Great job!")
  "B" -> println("Great job!")
  "C" -> println("You passed!")
  else -> println("Close! Be sure to prepare more next time!")
}
// print: Great job!
Range operator
KOTLIN
滚动查看更多
var height = 46 // inches

if (height in 1..53) {
  println("Sorry, you must be at least 54 inches to ride the coaster")
}
// Prints: Sorry, you must be at least 54 inches to ride the roller coaster
Equality Operators
KOTLIN
滚动查看更多
var myAge = 22
var sisterAge = 21

myAge == sisterAge // false
myAge != sisterAge // true

Null-Safety

Nullable vs Not-Nullable
KOTLIN
滚动查看更多
var a: String = "Kotlin" // a can never be null
a = null // compilation error
var b: String? = "Kotlin" // b can be null
b = null // ok
Safe-Calls
KOTLIN
滚动查看更多
val a = "Kotlin"
val b: String? = null
println(a.length) // can be called safely, because a is never null
println(b?.length) // b?.length returns the length of b, or null if b is null
println(a?.length) // Unnecessary safe call
Chaining Safe-Calls
KOTLIN
滚动查看更多
bob?.department?.head?.name // chain returns null if any property is null
Elvis Operator
KOTLIN
滚动查看更多
val l = b?.length ?: -1 // if b is null, return the default value -1
// equval to:
val l: Int = if (b != null) b.length else -1
Not Null Assertion Operator
KOTLIN
滚动查看更多
val l = b!!.length // throws a NullPointerException, if b is null

Collections

Immutable list
KOTLIN
滚动查看更多
var programmingLanguages = listOf("C#", "Java", "Kotlin", "Ruby")
Mutable List
KOTLIN
滚动查看更多
var fruits = mutableListOf("Orange", "Apple", "Banana", "Mango")
Access List
KOTLIN
滚动查看更多
var cars = listOf("BMW", "Ferrari", "Volvo", "Tesla")

println(cars[2]) // Prints: Volvo
Size Attribute
KOTLIN
滚动查看更多
var worldContinents = listOf("Asia", "Africa", "North America", "South America", "Antarctica", "Europe", "Australia")

println(worldContinents.size) // Prints: 7
List Manipulation
KOTLIN
滚动查看更多
var seas = listOf("Black Sea", "Caribbean Sea", "North Sea")
println(seas. contains("North Sea")) // Prints: true

// The contains() function performs a read operation on any list and determines if the element exists
seas.add("Baltic Sea") // Error: cannot write to immutable list
// The add() function can only be called on mutable lists, so the code above throws an error
Immutable Sets
KOTLIN
滚动查看更多
var primaryColors = setOf("Red", "Blue", "Yellow")
Mutable Sets
KOTLIN
滚动查看更多
var womenInTech = mutableSetOf("Ada Lovelace", "Grace Hopper", "Radia Perlman", "Sister Mary Kenneth Keller")
Access Collection Elements
KOTLIN
滚动查看更多
var companies = setOf("Facebook", "Apple", "Netflix", "Google")

println(companies.elementAt(3))
// Prints: Google
println(companies.elementAt(4))
// Returns and Error
println(companies.elementAtOrNull(4))
// Prints: null
Immutable Map
KOTLIN
滚动查看更多
var averageTemp = mapOf("winter" to 35,  "spring" to 60,  "summer" to 85, "fall" to 55)
Mutable Mapping
KOTLIN
滚动查看更多
var europeanDomains = mutableMapOf("Germany" to "de", "Slovakia" to "sk", "Hungary" to "hu", "Norway" to "no")
Retrieve map keys and values
KOTLIN
滚动查看更多
var oscarWinners = mutableMapOf("Parasite" to "Bong Joon-ho", "Green Book" to "Jim Burke", "The Shape Of Water" to "Guillermo del Toro")

println(oscarWinners.keys)
// Prints: [Parasite, Green Book, The Shape Of Water]

println(oscarWinners.values)
// Prints: [Bong Joon-ho, Jim Burke, Guillermo del Toro]
println(oscarWinners["Parasite"])
// Prints: Bong Joon-ho
Add and remove map entries
KOTLIN
滚动查看更多
var worldCapitals = mutableMapOf("United States" to "Washington D.C.", "Germany" to "Berlin", "Mexico" to "Mexico City", "France" to "Paris")

worldCapitals.put("Brazil", "Brasilia")
println(worldCapitals)
// Prints: {United States=Washington D.C., Germany=Berlin, Mexico=Mexico City, France=Paris, Brazil=Brasilia}

worldCapitals.remove("Germany")
println(worldCapitals)
// Prints: {United States=Washington D.C., Mexico=Mexico City, France=Paris, Brazil=Brasilia}

Function

Function
KOTLIN
滚动查看更多
fun greet() {
  println("Hey there!")
}

fun main() {
  //Function call
  greet() //Prints: Hey there!
}
Function Parameters
KOTLIN
滚动查看更多
fun birthday(name: String, age: Int) {
   println("Happy birthday $name! You turn $age today!")
}

fun main() {
  birthday("Oscar", 26)
  //Prints: Happy birthday Oscar! You turn 25 today!
  birthday("Amarah", 30)
  //Prints: Happy birthday Amarah! You turn 30 today!
}
Default Parameters
KOTLIN
滚动查看更多
fun favoriteLanguage(name: String, language: String = "Kotlin") {
  println("Hello, $name. Your favorite programming language is $language")
}

fun main() {
  favoriteLanguage("Manon")
  //Prints: Hello, Manon. Your favorite programming language is Kotlin

  favoriteLanguage("Lee", "Java")
  //Prints: Hello, Lee. Your favorite programming language is Java
}
Named Parameters
KOTLIN
滚动查看更多
fun findMyAge(currentYear: Int, birthYear: Int) {
   var myAge = currentYear -birthYear
println("I am $myAge years old.")
}

fun main() {
  findMyAge(currentYear = 2020, birthYear = 1995)
  //Prints: I am 25 years old.
  findMyAge(birthYear = 1920, currentYear = 2020)
  //Prints: I am 100 years old.
}
Return Statement
KOTLIN
滚动查看更多
//Return type is declared outside the parentheses
fun getArea(length: Int, width: Int): Int {
  var area = length *width

  //return statement
  return area
}

fun main() {
  var myArea = getArea(10, 8)
println("The area is $myArea.")
//Prints: The area is 80.
}
Single expression function
KOTLIN{.WRAP}
滚动查看更多
fun fullName(firstName: String, lastName: String) = "$firstName $lastName"

fun main() {
  println(fullName("Ariana", "Ortega"))
  //Prints: Ariana Ortega
  println(fullName("Kai", "Gittens"))
  //Prints: Kai Gittens
}
Function Literals
KOTLIN{.WRAP}
滚动查看更多
fun main() {
  //Anonymous Function:
  var getProduct = fun(num1: Int, num2: Int): Int {
return num1 *num2
  }
  println(getProduct(8, 3))
  //Prints: 24
//Lambda Expression
  var getDifference = { num1: Int, num2: Int -> num1 -num2 }
  println(getDifference(10, 3))
  //Prints: 7
}

Class

Class Example
KOTLIN
滚动查看更多
//class with properties containing default values
class Student {
  var name = "Lucia"
  var semester = "Fall"
  var gpa = 3.95
}

//shorthand syntax without class body
class Student
Class Instance
KOTLIN
滚动查看更多
// Class
class Student {
  var name = "Lucia"
  var semester = "Fall"
var gpa = 3.95
}

fun main() {
  var student = Student()
  // Instance
  println(student.name)
  // Prints: Lucia
  println(student.semester)
  // Prints: Fall
  println(student.gpa)
  // Prints: 3.95
}
Primary Constructor
KOTLIN
滚动查看更多
class Student(val name: String, val gpa: Double, val semester: String, val estimatedGraduationYear: Int)

fun main() {
  var student = Student("Lucia", 3.95, "Fall", 2022)
  println(student.name)
  //Prints: Lucia
  println(student.gpa)
  //Prints: 3.95
  println(student.semester)
  //Prints: Fall
  println(student.estimatedGraduationYear)
  //Prints: 2022
}
Initialization Block
KOTLIN
滚动查看更多
class Student(val name: String, val gpa: Double, val semester: String, val estimatedGraduationYear: Int) {
  init {
    println("$name has ${estimatedGraduationYear -2020} years left in college.")
  }
}

fun main() {
  var student = Student("Lucia", 3.95, "Fall", 2022)
//Prints: Lucia has 2 years left in college.
}
Member Function
KOTLIN
滚动查看更多
class Student(val name: String, val gpa: Double, val semester: String, val estimatedGraduationYear: Int) {

  init {
println("$name has ${estimatedGraduationYear -2020} years left in college.")
  }

  //member function
  fun calculateLetterGrade(): String {
    return when {
      gpa >= 3.0 -> "A"
      gpa >= 2.7 -> "B"
      gpa >= 1.7 -> "C"
gpa >= 1.0 -> "D"
      else -> "E"
    }
  }
}

//When the instance is created and the function is called, the when expression will be executed and return the letter grade
fun main() {
  var student = Student("Lucia", 3.95, "Fall", 2022)
//Prints: Lucia has 2 years left in college.
  println("${student.name}'s letter grade is ${student.calculateLetterGrade()}.")
  //Prints: Lucia's letter grade is A.
}

See Also

相关 Cheat Sheets

1v1免费职业咨询
logo

Follow Us

linkedinfacebooktwitterinstagramweiboyoutubebilibilitiktokxigua

We Accept

/image/layout/pay-paypal.png/image/layout/pay-visa.png/image/layout/pay-master-card.png/image/layout/pay-airwallex.png/image/layout/pay-alipay.png

地址

Level 10b, 144 Edward Street, Brisbane CBD(Headquarter)
Level 2, 171 La Trobe St, Melbourne VIC 3000
四川省成都市武侯区桂溪街道天府大道中段500号D5东方希望天祥广场B座45A13号
Business Hub, 155 Waymouth St, Adelaide SA 5000

Disclaimer

footer-disclaimerfooter-disclaimer

JR Academy acknowledges Traditional Owners of Country throughout Australia and recognises the continuing connection to lands, waters and communities. We pay our respect to Aboriginal and Torres Strait Islander cultures; and to Elders past and present. Aboriginal and Torres Strait Islander peoples should be aware that this website may contain images or names of people who have since passed away.

匠人学院网站上的所有内容,包括课程材料、徽标和匠人学院网站上提供的信息,均受澳大利亚政府知识产权法的保护。严禁未经授权使用、销售、分发、复制或修改。违规行为可能会导致法律诉讼。通过访问我们的网站,您同意尊重我们的知识产权。 JR Academy Pty Ltd 保留所有权利,包括专利、商标和版权。任何侵权行为都将受到法律追究。查看用户协议

© 2017-2025 JR Academy Pty Ltd. All rights reserved.

ABN 26621887572