Unary operator

Unary operators include prefix operators, increment and decrement operators, etc.

Prefix operator

The prefix operator precedes the operand, as shown below

expression Translated into
+a a.unaryPlus()
-a a.unaryMinus()
! a a.not()

Here is an example of an overloaded unary subtraction operator:

data class Point(val x: Int.val y: Int)

operator fun Point.unaryMinus(a) = Point(-x, -y)
Copy the code

Testing:

import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runners.JUnit4

@RunWith(JUnit4::class)
class OperatorDemoTest {

    @Test
    fun testPointUnaryMinus(a) {
        val p = Point(1.1)
        val np = -p
        println(np)
    }
}
// Point(x=-1, y=-1)
Copy the code

Increment and decrement operators

The inc() and dec() functions must return a value that is assigned to variables that use the ++ or — operations. Prefix and suffix expressions return different values. The specific values are as follows:

expression Translated into
a++ A.inc () returns a
a– A.dece () returns a
++a A. inic () returns a+1
–a A.dece () returns a-1

Binary operator

The binary operators in Kotlin include the arithmetic operator, index access operator, call operator, calculate and assign operator, equality and inequality operator, Elvis operator, comparison operator, infix operator, and so on. The following are introduced respectively.

Arithmetic operator

Kotlin’s arithmetic operators add, subtract, multiply, divide, complement, range operators, etc., as shown in the figure:

expression Translated into
a + b a.plus(b)
a – b a.minus(b)
a * b a.times(b)
a / b a.div(b)
a % b A.r em, arjun od (b) (b)
a.. b a.rangTo(b)

Custom overloaded “+” operator

The following uses the “+” operator of a Counter overloaded count class to increment the count of index. A code example is as follows:

data class Counter(val index: Int)

operator fun Counter.plus(increment: Int): Counter {
    return Counter(index + increment)
}
Copy the code

test

import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runners.JUnit4

@RunWith(JUnit4::class)
class OperatorDemoTest {
    
    @Test
    fun testCounterIndexPlus(a) {
        val c = Counter(1)
        val cPlus = c + 10
        println(cPlus)
    }
}
// Counter(index=11)
Copy the code

The in operator

The in operator is equivalent to the contains() function, as shown below.

expression Translated into
a in b b.contains(a)
a ! in b ! b.contains(a)

Index access operator

The index access operator square brackets [] is converted to call GET and set with the appropriate number of arguments, as shown below.

expression Translated into
a[i] a.get(i)
a[i] = b a.set(i, b)

Call operator

The parenthesis caller () is converted to the invoke() function, as is the argument call to the invoke() function. As shown below.

expression Translated into
a() a.invoke()
a(i) a.invoke(i)

Evaluates and assigns the operator

For assignment operations, such as a+=b, the compiler tries to generate code for a=a+b (including type checking: the type of a+b must be a subtype of A). The overloaded functions corresponding to the evaluate and assign operators are shown below.

expression Translated into
a += b a.plusAssign(b)
a -= b a.minusAssign(b)
a *= b a.timesAssign(b)
a /= b a.divAssign(b)
a %= b a.modAssign(b)

The equality and inequality operators

There are two types of equality in Koltin:

  • Reference equality ===! == (two references to the same object)
  • Structure equality ==! = (check by equals)
expression Translated into
a == b a? .equals(b) ? : (b === null)
a ! = b ! a(a? .equals(b) ? : (b === null))

The “==” operator is somewhat special: it is translated into a complex expression that filters for null values. Call equals(Any?) if a is not null. Function and returns its value: otherwise (that is, a === NULL) evaluates b === null and returns. When explicitly compared with NULL, a == NULL is automatically converted to a === NULL.

Comparison operator

All comparison expressions in Kotlin are converted to calls to the compareTo() function, which returns an Int value, as shown below.

expression Translated into
a>b a.compareTo(b) > 0
a<b a.compareTo(b) < 0
a>=b a.compareTo(b) >= 0
a<=b a.compareTo(b) <= 0

Customize the infix operator with the infix function

We can implement the infix operator by customizing the Infix function. A code example is as follows:

data class Person(val name: String, val age: Int)

infix fun Person.grow(years: Int): Person {
    return Person(name, age + years);
}
Copy the code

test

@RunWith(JUnit4::class)
class InfixFunctionDemoTest {

    @Test
    fun testInfixFunction(a) {
        val person = Person("Jack".20)
        println(person.grow(2)) // Call the function directly
        println(person grow(2)) // Infix expression invocation}}// Person(name=Jack, age=22)
// Person(name=Jack, age=22)
Copy the code