Kotlina operatori: aritmētika, uzdevumu operators un citi

Kotlinam ir operatoru kopums, lai veiktu aritmētiku, piešķiršanu, salīdzināšanas operatorus un daudz ko citu. Jūs iemācīsities izmantot šos operatorus šajā rakstā.

Operatori ir īpaši simboli (rakstzīmes), kas veic operācijas (mainīgos un vērtības). Piemēram, +ir operators, kurš veic papildinājumu.

Rakstā Java mainīgie jūs iemācījāties deklarēt mainīgos un piešķirt mainīgajiem vērtības. Tagad jūs iemācīsities izmantot operatorus, kas ar viņiem veic dažādas darbības.

1. Aritmētiskie operatori

Šeit ir Kotlinas aritmētisko operatoru saraksts:

Kotlina aritmētiskie operatori
Operators Nozīme
+ Papildinājums (lieto arī virkņu savienošanai)
- Atņemšanas operators
* Reizināšanas operators
/ Divīzijas operators
% Moduļu operators

Piemērs: Aritmētiskie operatori

 fun main(args: Array) ( 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") ) 

Palaidot programmu, izeja būs:

 skaitlis1 + skaitlis2 = 16,0 skaitlis1 - skaitlis2 = 9,0 skaitlis1 * skaitlis2 = 43,75 skaitlis1 / skaitlis2 = 3,5714285714285716 skaitlis1% skaitlis2 = 2,0

+Operators izmanto arī, lai konkatenācija Stringvērtībām.

Piemērs: virkņu savienošana

 fun main(args: Array) ( val start = "Talk is cheap. " val middle = "Show me the code. " val end = "- Linus Torvalds" val result = start + middle + end println(result) )

Palaidot programmu, izeja būs:

Saruna ir lēta. Parādiet man kodu. - Linuss Torvalds

Kā faktiski darbojas aritmētiskie operatori?

Pieņemsim, ka jūs izmantojat +aritmētisko operatoru, lai pievienotu divus skaitļus a un b.

Zem pārsega izteiksme a + bizsauc a.plus(b)dalībnieka funkciju. plusOperators ir pārslogots uz darbu ar Stringvērtībām un citām pamata datu tipi (izņemot Char un Būla).

 // + operators pamata veidiem operators fun plus (cits: baits): Int operators fun plus (cits: īss): Int operators fun plus (cits: Int): Int operators fun plus (cits: garš): Long operator fun plus (cits: pludiņš): pludiņa operatora izklaide plus (cita: dubultā): dubultā // virkņu savienošanas operatora jautrībai String? .plus (cits: kāds?): virkne 

+Operatoru var izmantot arī darbam ar lietotāja definētiem tipiem (piemēram, objektiem), pārslogojot plus()funkciju.

Ieteicamā literatūra: Kotlin operatora pārslodze

Šeit ir aritmētisko operatoru un tiem atbilstošo funkciju tabula:

Izteiksme Funkcijas nosaukums Tulkots uz
a + b plus a. plus (b)
a - b mīnus a. mīnus (b)
a * b reizes a. reizes (b)
a / b div a.div (b)
a% b mod a. mod. b)

2. Uzdevumu operatori

Piešķiršanas operatori tiek izmantoti, lai mainīgajam piešķirtu vērtību. Mēs jau iepriekš izmantojām vienkāršu piešķiršanas operatoru =.

 val vecums = 5

Šeit 5 tiek piešķirti mainīgam vecumam, izmantojot =operatoru.

Šeit ir visu piešķiršanas operatoru un tiem atbilstošo funkciju saraksts:

Izteiksme Līdzvērtīgs Tulkots uz
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)

Piemērs: uzdevumu operatori

 fun main(args: Array) ( var number = 12 number *= 5 // number = number*5 println("number = $number") )

Palaidot programmu, izeja būs:

 skaitlis = 60

Ieteicamā literatūra: Kotlinas uzdevumu operatoru pārslodze.

3. Unārija prefiksu un pieauguma / samazināšanas operatori

Here's a table of unary operators, their meaning, and corresponding functions:

Operator Meaning Expression Translates to
+ Unary plus +a a.unaryPlus()
- Unary minus (inverts sign) -a a.unaryMinus()
! not (inverts value) !a a.not()
++ Increment: increases value by1 ++a a.inc()
-- Decrement: decreases value by 1 --a a.dec()

Example: Unary Operators

 fun main(args: Array) ( 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") )

When you run the program, the output will be:

 -a = -1 !b = false --c = 0

Recommended Reading: Overloading Unary Operators

4. Comparison and Equality Operators

Here's a table of equality and comparison operators, their meaning, and corresponding functions:

Operator Meaning Expression Translates to
> greater than a> b a.compareTo(b)> 0
< less than a < b a.compareTo(b) < 0
>= greater than or equals to a>= b a.compareTo(b)>= 0
<= less than or equals to a < = b a.compareTo(b) <= 0
== is equal to a == b a?.equals(b) ?: (b === null)
!= not equal to a != b !(a?.equals(b) ?: (b === null))

Comparison and equality operators are used in control flow such as if expression , when expression , and loops .

Example: Comparison and Equality Operators

 fun main(args: Array) ( 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") ) 

When you run the program, the output will be:

 b is larger than a. max = 12 

Recommended Reading: Overloading of Comparison and Equality Operators in Kotlin

5. Logical Operators

There are two logical operators in Kotlin: || and &&

Here's a table of logical operators, their meaning, and corresponding functions.

Operator Description Expression Corresponding Function
|| true if either of the Boolean expression is true (a>b)||(a (a>b)or(a
&& true if all Boolean expressions are true (a>b)&&(a (a>b)and(a

Note that, or and and are functions that support infix notation.

Logical operators are used in control flow such as if expression , when expression , and loops .

Example: Logical Operators

 fun main(args: Array) ( 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) )

When you run the program, the output will be:

 true

Recommended Reading: Overloading of Logical Operators in Kotlin

6. in Operator

The in operator is used to check whether an object belongs to a collection.

Operator Expression Translates to
in a in b b.contains(a)
!in a !in b !b.contains(a)

Example: in Operator

 fun main(args: Array) ( val numbers = intArrayOf(1, 4, 42, -3) if (4 in numbers) ( println("numbers array contains 4.") ) )

When you run the program, the output will be:

 numbers array contains 4.

Recommended Reading: Kotlin in Operator Overloading

7. Index access Operator

Here are some expressions using index access operator with corresponding functions in Kotlin.

Expression Translated to
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)

Example: Index access Operator

 fun main(args: Array) ( val a = intArrayOf(1, 2, 3, 4, - 1) println(a(1)) a(1)= 12 println(a(1)) ) 

When you run the program, the output will be:

 2 12

Recommended Reading: Kotlin Index access operator Overloading

8. Invoke Operator

Šeit ir daži izteicieni, izmantojot Kotlin operētājsistēmu ar atbilstošām funkcijām.

Izteiksme Tulkots uz
a() a.invoke()
a(i) a.invoke(i)
a(i1, i2,… , in) a.inkove(i1, i2,… , in)
a(i) = b a.set(i, b)

Kotlīnā iekavas tiek tulkotas kā invokedalībnieka izsaukuma funkcija.

Ieteicams lasīt: Izsaukt operatora pārslodzi Kotlīnā

Darbība pa bitēm

Atšķirībā no Java Kotlinā nav bitu un bitu maiņas operatoru. Lai veiktu šo uzdevumu, tiek izmantotas dažādas funkcijas (atbalsta papildinājumu apzīmējumus):

  • shl - Parakstīta maiņa pa kreisi
  • shr - Parakstīts maiņa pa labi
  • ushr - neparakstīta maiņa pa labi
  • and - Bitwise un
  • or - Bitwise vai
  • xor - Bitu virzienā xor
  • inv - Apgriešana pa bitiem

Apmeklējiet šo lapu, lai uzzinātu vairāk par Bitwise operācijām Kotlinā.

Tāpat Kotlīnā nav trīslīmeņu operatora, atšķirībā no Java.

Interesanti raksti...