Operators in all languages are basically the same, except that the +, -, *, and /. Operators are special symbols (characters) that perform operations on operands (variables and values). For example, + is the operator that performs addition.

Arithmetic operator

The operator describe
  • | addition operator (user connection string splicing) can also be

– * | | subtraction operator multiplication operator / | division operator (rounded) % | the modulus operator (modulo)

Here is an example of how these operators work

Println (" 5 + 2 = "+ (5) + 2) println (" 5-2 =" + (5 -) 2) println (" 5 * 2 = "+ (5 * 2) println (" 5/2 =" + (5/2)) println (" 5% 2 = "+ 2) (5%) the output 5 + 2 = 7 5-2 = 3 * 2 = 10 5/2 = 5% 2 = 1Copy the code

If both sides of the/and % operators are integers, the divisor cannot be 0, or the compiler will fail. What happens when the divisor is 0 or 0.0?

Println (" 5/0.0 = "+ (5/0.0)) println (" 5% 0.0 =" + (5%) 0.0) println (" 5.0/0 = "+ (5.0/0)) println (" 5.0% 0 =" + 0) (5.0%) the output: 0.0 = 5/0.0 = 5% Infinity NaN 5.0/0 = 5.0% Infinity 0 = NaNCopy the code

Infinity stands for Infinity and NaN for non-number

Assignment operator ‘=’

The assignment operator assigns a value to a variable. To the left of “=” is the variable to be assigned, and to the right of “=” is the value to be assigned. It can be a literal (var a=5,var b = 6), an expression (var x= a+b), or the return value of a function.

Prefix operator

The operator describe The sample
  • | yuan and | + a

– minus | | a yuan – a! | invert |! B + + | increasing: + 1 | + + a + or – | decline: – | – a or a – 1

var a = 5 var b = -3 println("+a=" + (+a)) println("-a=" + (-a)) println("+b=" + (+b)) println("-b=" + (-b)) Println (" + + a = "+ + + (a)) println (" + =" + (+)) run results: + a = 5 + b = = 5 - a - 3 - b = 3 + a + a = 6 + =Copy the code

The unary operator is the arithmetic of positive and negative, positive to negative, negative to positive, and “!” If you add ++ to a single variable, the value of the input is the same. If you add 1 to a single variable, what does that look like in arithmetic

Var a = 5 println (" + + 2 = "+ + + 2)) println (" a =" + a) input the result is: + 7 + 2 = a = 6 var a = 5 println (" + + a + 2 = "+ + + a + 2)) println (" a =" + a) input the result: + + a + 2 = 8 = 6 aCopy the code

For the variable itself, the variable increases by 1 when ++ is before and after, but in the expression, it is different. Before, the variable participates in the expression calculation after increasing by 1, and after ++, the variable first participates in the expression calculation, and then increases by 1. So you end up with the same input to variable A and you add +1 to that, and you get a different expression.

Comparison operator

The result of the comparison operator is a Boolean value, true or false, either true or false. It is used mostly in conditional statements such as if or when:

    if (a>b){
        
    }else{
        
    }
Copy the code
The operator describe The sample

| greater than | a > b = | is greater than or equal to | a > = b

< | less than | < b < = | less than or equal to | a < = b = = is | | a = = b. = | is not equal to | a! =b

var a = 5 var b = 6 println("a="+a) println("b="+b) println("(a>b)="+(a>b)) println("(a>=b)="+(a>=b)) println("(a<b)="+(a<b)) println("(a<=b)="+(a<=b)) println("(a! =b)="+(a! = b) output: a = 5 b = 6 (a > b) = false (> = b) = false (a < b) = true (a < = b) = true (a! =b)=trueCopy the code

Logical operator

Kotlin logical operators have two “| |” and “&”

The operator describe The sample
&& The result is true if all expressions are true and false otherwise a&&b
∣ ∣ As long as one expression is true, it’s true otherwise it’s false A ∣ ∣ b
Var = 5 var b = 6 if (a > b | | a = = b) {println (" a > b | | = = b is true ")} else {println (" a > b | | = = b is false ")}Copy the code

And in! The in operator.

In English = in English = in English “In” means, in fact, that is what it means, indicating whether a value or variable is in a set.

Next we define a character ‘s’ and a string ‘string’ to see if the character S is in the string string

var a = 'i' var b = "string" println("s in string:"+ (a in b)) println("s ! in string:"+ (a ! S in string:true s! in string:falseCopy the code

We use in to determine if s is in a string and return true. In returns false

Index operator

The index allows us to obtain or modify the value of the index, which starts at 0

    var b = "string"
    println(b[2])
Copy the code

So in the code above we’re going to take the string index of the character 2, which is the third character, and return ‘r’.