Kotlin summary

A, Kotlin basic grammar

1, Kotlin is a statically typed programming language that runs on the java virtual machine. Kotlin program files .kt end

 

2, the function is defined:

  The function definition for fun, in the format of parameters: Parameter Type

  

  Function returning 2.1

  fun sum (a: Int, b: Int): Int {// function returning

    return a+b

  }

  

  Function has no return value 2.2

  fun sum (a: Int, b: Int) {// No Return Value Function

    print(a+b)   

  }

 

  2.3 Variable-length argument available vararg keyword representation (for variable length means that there is a similar operation)

  fun varsfun(vararg var:Int){

    for(vt in var){

      println(vt)

    }

  }

  fun main(args:Array<String>){

    varsfun(1,2,3)

  }  

  operation result:

  

 

 

 

3, define constants and variables

  var define variables, val define constants (val similar role in java final)

  var <identifier>: <type> = <initialization value> var a: Int = 1 is equivalent to var a = 1 a + = 1 // variables can be modified

  val <identifier>: <type> = <initialization value> val b: Int = 1 // not executable b b + = 1 can not be modified because the statement val

 

4, String template

  $ Represents a variable name or the variable value

  $ VarName value of the variable

  $ {VarName.fun ()} The method returns a variable representing the value inside {} is the expression

  

 

 

  

       

 

 

 

5, Null checking mechanism

  Kotlin air safety design for declared as empty parameters when using an empty judging process, there are two approaches:

  ① !! parameters if the declaration is null Throws similar java as a null pointer

       ② ? Not deal with the return value is null or :? Short judgment processing

 

6, the type of detection and automatic conversion

  is the operator type determination made, the target is judged to be automatically converted into a type of

 

7, interval

  Interval expression operators form ..

for (i in 1..4) print(i) // 输出“1234”

for (I in  . 4 .. . 1 ) Print (I) // nothing output

if (i in 1..10) { // 等同于 1 <= i && i <= 10
    println(i)
}

// use the step designated step 
for (I in  . 1 .. . 4 step 2 ) Print (I) // outputs "13"

for (i in 4 downTo 1 step 2) print(i) // 输出“42”

 

8, comparing two numbers

  == size comparison

  === comparison address

fun main(args: Array<String>) {
    A Val: Int = 10000 
    the println (A === A) // to true, values are equal, the object is equal to the address

    // After packing up, creating two different objects 
    Val boxedA:? Int = A
    val anotherBoxedA: Int? = a

    // even after packing, but the values are equal, are 10000 
    the println (boxedA === anotherBoxedA) //   to false, values are equal, the object address is not the same 
    the println (boxedA == anotherBoxedA) // to true, the value is equal to 
}

 

9, the operator position and

  shl - left shift (the equivalent of java <<)

  shr - right shift (corresponding to the java >>)

  ushi - unsigned left shift

  ushr - unsigned right shift

  and - operating

  or - or operation

  xor - exclusive-OR operation

  inv - negate operation

Two, Kotlin basic data types

Types of Bit Width
Double 64
Float 32
Long 64
Int 32
Short 16
Byte 8

  kotlin not char character is numeric, is a separate data type

 

  1, character

  Kotlin char must be in single quotes '' comprises up, for example '0', 'a'

  

  2, array

  Array An array type implemented using ArrayOf () Create: var a = arrayOf (1,2,3)

 

  3, the string ""

  Kotlin supports three quotes "" "string enclosed supports multi-line character string, such as:

 

 

 

 

Third, the control condition

  1, if - and java similar, does not describe

  

  2, when (corresponding to the java switch)

  

 

   else the default switch is equivalent to

 

  If many branches need to be addressed in the same way, it is possible to put together a plurality of branch conditions, separated by commas

when (x) {
    0, 1 -> print("x == 0 or x == 1")
    else -> print("otherwise")
}

 

 

  May be detected when using a value (in) or not (! In) a set interval or

when (x) {
    in 1..10 -> print("x is in the range")
    in validNumbers -> print("x is valid")
    !in 10..20 -> print("x is outside the range")
    else -> print("none of the above")
}

 

 

 

 Fourth, the control loop

  1, for traversal cycle one is directly and the other is the use of an index list as shown in FIG.

    

 

   

  2、while 与do..while

  

 

Guess you like

Origin www.cnblogs.com/enhance/p/11584498.html