<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                # Kotlin 運算符 > 原文: [https://www.programiz.com/kotlin-programming/operators](https://www.programiz.com/kotlin-programming/operators) #### Kotlin 有一組運算符,可以執行算術,賦值,比較運算符等。 您將在本文中學習如何使用這些運算符。 運算符是對操作數(變量和值)進行運算的特殊符號(字符)。 例如,`+`是執行加法的運算符。 在 [Java 變量](/kotlin-programming/variable-types "Kotlin Variables")文章中,您學習了聲明變量并為變量賦值。 現在,您將學習使用運算符對它們執行各種操作。 * * * ## 1.算術運算符 以下是 Kotlin 中的算術運算符列表: Kotlin 算術運算符 | 運算符 | 含義 | | --- | --- | | `+` | 加法(也用于字符串連接) | | `-` | 減法運算符 | | `*` | 乘法運算符 | | `/` | 除法運算符 | | `%` | 模運算符 | * * * ### 示例:算術運算符 ```kt fun main(args: Array<String>) { val number1 = 12.5 val number2 = 3.5 var result: Double result = number1 + number2 println("number1 + number2 = $result") result = number1 - number2 println("number1 - number2 = $result") result = number1 * number2 println("number1 * number2 = $result") result = number1 / number2 println("number1 / number2 = $result") result = number1 % number2 println("number1 % number2 = $result") } ``` 運行該程序時,輸出為: ```kt number1 + number2 = 16.0 number1 - number2 = 9.0 number1 * number2 = 43.75 number1 / number2 = 3.5714285714285716 number1 % number2 = 2.0 ``` `+`運算符還用于`String`值的連接。 * * * ### 示例:字符串的連接 ```kt fun main(args: Array<String>) { val start = "Talk is cheap. " val middle = "Show me the code. " val end = "- Linus Torvalds" val result = start + middle + end println(result) } ``` 運行該程序時,輸出為: ```kt Talk is cheap. Show me the code. - Linus Torvalds ``` * * * ### 算術運算符實際上如何工作? 假設您正在使用`+`算術運算符將兩個數字`a`和`b`相加。 在后臺,表達式`a + b`調用`a.plus(b)`成員函數。`plus`運算符被重載以使用`String`值和其他基本數據類型( [Char](/kotlin-programming/variable-types#char "Kotlin Char Type") 和[布爾](/kotlin-programming/variable-types#boolean "Kotlin Boolean Type")除外)。 ```kt // + operator for basic types operator fun plus(other: Byte): Int operator fun plus(other: Short): Int operator fun plus(other: Int): Int operator fun plus(other: Long): Long operator fun plus(other: Float): Float operator fun plus(other: Double): Double // for string concatenation operator fun String?.plus(other: Any?): String ``` 您也可以通過重載`plus()`函數,使用`+`運算符處理用戶定義的類型(如對象)。 **推薦閱讀**: [Kotlin 運算符重載](/kotlin-programming/operator-overloading "Kotlin Operator Overloading") 這是算術運算符及其對應函數的表: | 表達式 | 函數名稱 | 轉換為 | | --- | --- | --- | | `a + b` | 加法 | `a.plus(b)` | | `a - b` | 減法 | `a.minus(b)` | | `a * b` | 乘法 | `a.times(b)` | | `a / b` | 除法 | `a.div(b)` | | `a % b` | 取余 | `a.mod(b)` | * * * ## 2.賦值運算符 賦值運算符用于為變量賦值。 之前我們已經使用了簡單的賦值運算符`=`。 ```kt val age = 5 ``` 在此,使用`=`運算符將 5 分配給變量`age`。 以下是所有賦值運算符及其對應函數的列表: | 表達式 | 相當于 | 轉化為 | | --- | --- | --- | | `a += b` | `a = a + b` | `a.plusAssign(b)` | | `a -= b` | `a = a - b` | `a.minusAssign(b)` | | `a *= b` | `a = a * b` | `a.timesAssign(b)` | | `a /= b` | `a = a / b` | `a.divAssign(b)` | | `a %= b` | `a = a % b` | `a.modAssign(b)` | * * * ### 示例:賦值運算符 ```kt fun main(args: Array<String>) { var number = 12 number *= 5 // number = number*5 println("number = $number") } ``` 運行該程序時,輸出為: ```kt number = 60 ``` **推薦閱讀**: *重載 Kotlin 中的賦值運算符*。 * * * ## 3.一元前綴和遞增/遞減運算符 這是一元運算符,它們的含義和相應函數的表: | 運算符 | 含義 | 表達式 | 轉換為 | | --- | --- | --- | --- | | `+` | 一元加 | `+a` | `a.unaryPlus()` | | `-` | 一元減(反轉符號) | `-a` | `a.unaryMinus()` | | `!` | 否定(反轉值) | `!a` | `a.not()` | | `++` | 遞增:值加 1 | `++a` | `a.inc()` | | `--` | 遞減:值減 1 | `--a` | `a.dec()` | * * * ### 示例:一元運算符 ```kt fun main(args: Array<String>) { val a = 1 val b = true var c = 1 var result: Int var booleanResult: Boolean result = -a println("-a = $result") booleanResult = !b println("!b = $booleanResult") --c println("--c = $c") } ``` 運行該程序時,輸出為: ```kt -a = -1 !b = false --c = 0 ``` **推薦閱讀**: *重載一元運算符* * * * ## 4.比較和相等運算符 這是一張相等和比較運算符,它們的含義以及相應函數的表: | 運算符 | 含義 | 表達式 | 轉換為 | | --- | --- | --- | --- | | `>` | 大于 | `a > b` | `a.compareTo(b) > 0` | | `<` | 小于 | `a < b` | `a.compareTo(b) < 0` | | `>=` | 大于或等于 | `a >= b` | `a.compareTo(b) >= 0` | | `<=` | 小于或等于 | `a <= b` | `a.compareTo(b) <= 0` | | `==` | 等于 | `a == b` | `a?.equals(b) ?: (b === null)` | | `!=` | 不等于 | `a != b` | `!(a?.equals(b) ?: (b === null))` | `if`和`when`表達式,和循環在控制流中使用比較和相等運算符。 * * * ### 示例:比較和相等運算符 ```kt fun main(args: Array<String>) { val a = -12 val b = 12 // use of greater than operator val max = if (a > b) { println("a is larger than b.") a } else { println("b is larger than a.") b } println("max = $max") } ``` 運行該程序時,輸出為: ```kt b is larger than a. max = 12 ``` **推薦閱讀**: *Kotlin 中比較和相等運算符的重載* * * * ## 5.邏輯運算符 Kotlin 中有兩個邏輯運算符:`||`和`&&` 這是邏輯運算符,它們的含義和相應函數的表。 | 運算符 | 描述 | 表達式 | 對應函數 | | --- | --- | --- | --- | | <code>&#124;&#124;</code> | 如果任一布爾表達式為`true`,則為`true` | `(a>b)&#124;&#124;(a<c)` | `(a>b)or(a<c)` | | `&&` | 如果所有布爾表達式均為`true`,則為`true` | `(a>b)&&(a<c)` | `(a>b)and(a<c)` | 注意,`or`和`and`是支持[中綴表示法](https://en.wikipedia.org/wiki/Infix_notation)的函數。 `if`和`when`表達式,和循環在控制流中使用邏輯運算符。 * * * ### 示例:邏輯運算符 ```kt fun main(args: Array<String>) { val a = 10 val b = 9 val c = -1 val result: Boolean // result is true is a is largest result = (a>b) && (a>c) // result = (a>b) and (a>c) println(result) } ``` 運行該程序時,輸出為: ```kt true ``` **推薦閱讀**: *Kotlin 中邏輯運算符的重載* * * * ## 6\. `in`運算符 `in`運算符用于檢查對象是否屬于集合。 | 運算符 | 表達式 | 轉換為 | | --- | --- | --- | | `in` | `a in b` | `b.contains(a)` | | `!in` | `a !in b` | `!b.contains(a)` | * * * ### 示例:`in`運算符 ```kt fun main(args: Array<String>) { val numbers = intArrayOf(1, 4, 42, -3) if (4 in numbers) { println("numbers array contains 4.") } } ``` 運行該程序時,輸出為: ```kt numbers array contains 4. ``` **推薦閱讀**: *運算符重載中的 Kotlin* * * * ## 7.索引訪問運算符 以下是在 Kotlin 中使用帶有索引訪問運算符和相應函數的一些表達式。 | 表達式 | 轉換為 | | --- | --- | | `a[i]` | `a.get(i)` | | `a[i, n]` | `a.get(i, n)` | | `a[i1, i2, ..., in]` | `a.get(i1, i2, ..., in)` | | `a[i] = b` | `a.set(i, b)` | | `a[i, n] = b` | `a.set(i, n, b)` | | `a[i1, i2, ..., in] = b` | `a.set(i1, i2, ..., in, b)` | * * * ### 示例:索引訪問運算符 ```kt fun main(args: Array<String>) { val a = intArrayOf(1, 2, 3, 4, - 1) println(a[1]) a[1]= 12 println(a[1]) } ``` 運行該程序時,輸出為: ```kt 2 12 ``` **推薦閱讀**: *Kotlin 索引訪問運算符重載* * * * ## 8.調用運算符 以下是在 Kotlin 中使用帶有相應函數的調用操作符的一些表達式。 | 表達式 | 轉換為 | | --- | --- | | `a()` | `a.invoke()` | | `a(i)` | `a.invoke(i)` | | `a(i1, i2, ..., in)` | `a.inkove(i1, i2, ..., in)` | 在 Kotlin 中,括號被翻譯為調用`invoke`成員函數。 **推薦閱讀**: *調用 Kotlin 中的運算符重載* * * * ### 按位運算 與 Java 不同,Kotlin 中沒有按位和移位運算符。 為了執行這些任務,使用了各種函數(支持中綴符號): * `shl` - 符號左移 * `shr` - 符號右移 * `ushr` - 無符號右移 * `and` - 按位和 * `or` - 按位或 * `xor` - 按位異或 * `inv` - 按位反轉 訪問此頁面以了解有關 Kotlin 中[按位運算的更多信息](/kotlin-programming/bitwise "Kotlin Bitwise Operations")。 * * * 另外,與 Java 不同,Kotlin 中沒有三元運算符。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看