This is the 12th day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021

One, agreed

Predetermined: That is, Kotlin allows us to provide a predefined set of operators for our types. These operators have fixed symbolic representations (such as + or *) and fixed priorities. To implement such operators, we provide a fixed name [function] for the corresponding operation type. Such techniques are called conventions

Because the set of interfaces implemented by a class is fixed, and Kotlin cannot modify an existing class to implement other interfaces, it is generally implemented through the mechanism of extending functions to add new convention methods to existing classes to accommodate any existing Java classes.

Operators and operator overloading

There are two types of operation according to the number of operation data:

  1. Unary operation: that is, there is only one operand
  2. Binary operation: that is, there are two or more operands. Special note: in the presence of multiple operands, the compound operation or split into multiple operations.

2.1. Unitary operation

Unary operation: refers to the case of one operand,

2.1.1. Simple unitary operation

There are three unary operations in three cases:

  • +Represents implementing one for operandsA plus signThe operand is of numeric type
  • -Represents implementing one for operandsMinus signThe operand is of numeric type
  • !Denotes the meaning of invert, and its operand isbooleantype

Provide a table for visual display:

The operator overloading
+a a.unaryPlus()
-a a.unaryMinus()
! a a.not()

Ex. :

Var a = 1 var b = -2 var c = true var d = false println("+a = ${+a}\t -a = ${-a}\t! c = ${! c}") println("+b = ${+b}\t -b = ${-b}\t ! d = ${! D} ") / / operator overloading println (" + a = ${a.u naryPlus ()} \ t - a = ${a.u naryMinus ()} \ t! c = ${c.not()}") println("+b = ${b.unaryPlus()}\t -b = ${b.unaryMinus()}\t ! D = ${d.n ot ()} "),Copy the code

The output is:

+a = 1 -a = -1 ! c = false +b = -2 -b = 2 ! d = true +a = 1 -a = -1 ! c = false +b = -2 -b = 2 ! d = trueCopy the code
2.1.2 Complex unitary operation

A complex unary operator is one that increments and decays its operands. It’s the same with Java

There are four main cases:

  • Suffix increment: indicates that the operand is incremented. The operand is numeric. Such as:a++
  • Suffix decrement: indicates that the operand is decrement. The operand is numeric. Such as:a--
  • Prefix increment: the prefix increment operation is performed on the operand. The operand is numeric. Such as:++a
  • Prefix decrement: the prefix is incremented by its operands. The operands are numeric. Such as:--a

Provide a table for visual display:

The operator overloading said
a++ a.inc() a = a.also{ a.inc() }
a– a.dec() a = a.also{ a.dec() }
++a a.inc() a = a.inc().also{ a = it }
–a a.dec() a = a.dec().also{ a = it }

Explanation: the ++ operator is overloaded with inc() and the — operator is overloaded with dec(). But there is a clear difference between prefix and suffix operations:

  • The suffix operationIt doesn’t execute itself on the first call. The increment or decrement operation is performed at the second start.
  • The prefix operationIncrement or decrement is performed on the first call

Example:

Var = 10 var b = 10 var c = 10 var d = 10 / / operators realize println (" + = ${+} \ t, b = $- {b} \ t + + c = ${+ + c} \ t - d = ${--d}"); ${--d}"); A.so {a.inc()} b.so {b.di c()} c.i NC (). Also {c = it} d.di C (). Also {d = it} println("a = $a \t b = $b \t c = $c \t d = $d")Copy the code

The output is:

a++ = 10     b-- = 10    ++c = 11    --d = 9
a = 10   b = 10      c = 11      d = 9
Copy the code

2.2 Binary Operation

Binary operation: that is, there are two or more operands.

2.2.1. Simple binary operation

Simple binary operations are:

  • a + b, represents the addition of two operands. It is noteworthy that if one of the operands isStringType. The return value isStringType, the return value will be numeric if and only if both operands are numeric.
  • a - b, indicates that the two operands are subtracted and the return value is numeric
  • a * bRepresents the multiplication of two operands and returns a numeric value
  • a / b, indicates that the two operands are divided and the return value is numeric
  • a % b, represents the remainder of the division of two operands, officially calleddie, i.e.,aDie tob. The return value isInttype
  • a .. b, represents the range (interval), which is not specified here, but is explained with the interval operator at the following point.

Here is a table for visual representation:

The operator overloading
a + b a.plus(b)
a – b a.minus(b)
a * b a.tiems(b)
a / b a.div(b)
a % b Or arjun od a.r em (b) (b)
a .. b a.rangTo(b)

It is worth noting here that the overload of a % b is a.rem() or a.mod(). However, a.mod() is a deprecated version of Koltin1.0 overload method. Koltin1.1 and above use a.rem() overload method

case

Println ("a + d = "+ a + d) println("c + d =" + c) println("c + d = "+ c  + d) println("a + b = ${a + b} \t a - b = ${a - b} \t a * b = ${a * b} \t a / b = ${a / b} \t a % b = ${a % b}") // Println ("a + d = ${a + d}") Is numeric string template limited println (" a + b = ${Amy polumbo lus (b)} \ t a - b = ${arjun inus (b)} \ t a * b = ${a.t imes (b)} \ t a/b = ${a. d. iv (b)} \ t A % b = ${a.property (b)}") // println(a.property (d)) {a % b = ${a.property (b)}"); Because the plus() method takes arguments of super (Any) typeCopy the code

The output is:

a + d = 10Kotlin c + d = 2Kotlin a + b = 12 a - b = 8 a * b = 20 a / b = 5 a % b = 0 a + b = 12 a - b = 8 a * b = 20 a /  b = 5 a % b = 0Copy the code

2.2.2 Compound binary operation

The compound binary operations are:

  • a += b, the value of the first operand is the first operand plus the second operandStringType. The return value isStringType, the return value will be numeric if and only if both operands are numeric.
  • a -= b, the value of the first operand is the first operand minus the second operand. The return value is numeric
  • a *= b, the value of the first operand is the first operand times the second operand, and the return value is numeric
  • a /= bRepresents the value of the first operand divided by the second operand and returns a numeric value
  • a %= b, representing the value of the first operand and modulo of the first operand and the second operand. The return value isInttype

Here is a table for visual representation:

The operator said overloading
a += b a = a + b a = a.plus(b)
a -= b a = a – b a = a.minus(b)
a *= b a = a * b a = a.tiems(b)
a /= b a = a / b a = a.div(b)
a %= b a = a % b a = a.rem(b)

Example: operator implementation

Var = 2 b var var c = a = 10 "Kotlin / / main demonstration of the string" c + + = = a equivalent to the c = c.p lus (a) print (" \ t "c = $c) equivalent to a = a + = b Amy polumbo lus (b) Print ("a = $a \t") print("a = $a \t") print("a = $a \t") print("a = $a \t") print("a = $a \t") print("a = $a \t" \t") print("a = $a \t") print("a = $a \t") print("a = $a \t")Copy the code

The output is:

c = Kotlin10    a = 12  a = 8   a = 20  a = 5   a = 0   
Copy the code

You might say why isn’t there Kotlin’s version here? You might have a += b <=> a.plassign () while reading official documents or other people’s blog posts. This is not how a.plusassign () is used. Primitives. Kt does not exist in this file. Because assignment in Koltin is not an expression. That is, a += b <=> A = a + b in Kotlin is a = A.plus (b). Arrays and collections, however, have both the plus() and plusAssign() functions.

One more thing: if my first operand is defined as val(immutable), the expression a += b will compile wrong.

There are no plusAssign(), minusAssign(), timesAssign(), divAssign() or remAssign() methods in the source code primitievs.kt file. So why do official documents exist? I won’t go into details here, but I will show you when I customize overloaded operator methods. Please go down in detail to see some more advanced operations

2.3. Bit operation

Bit operation operation: namely to a number of displacement operation.

2.4. Interval operation

Interval operators: symbols.. . Note that this operator does not exist in Java, and both operands are integers

The operator said overloading
a .. b Between a and B a.rangeTo(b)

This operator is commonly used in for loops and is occasionally used in conditional judgments.

Ex. :

Val a = 1 val b = 5 Println ("s = $s") for (index in a.. Println ("t = $t") for (index in a.rangeto (b)){val t = 3 in a.rangeto (b) println("t = $t") for (index in a.rangeto (b)){ print("index = $index \t") }Copy the code

The output is:

s = true
index = 1   index = 2   index = 3   index = 4   index = 5   
t = true
index = 1   index = 2   index = 3   index = 4   index = 5 
Copy the code