Note: the current document is a summary of Kotlin’s self-study, if there is any ambiguous meaning, please advise, thank you: -). Next Kotlin.2 – The structure and classification of classes

Directory: - Packet - Current packet - Guide Packet - Data type - Basic Data type - Char - Boolean - Array - String - Common class, interfaceCopy the code

Packet – Current packet and guide packet

1. The current package

Such as:

package demo
Copy the code

2. The package

You can use AS to specify a new name for a class without creating a new class when importing the same class name.

Such as:

Import foo.Bar // Bar is accessible import bar.Bar as bBar // bBar stands for "bar.bar"Copy the code

The data type

  • Basic data typesByte,Short,Int,Long,Float,Double

    • Bytes than (unit – a – > bytes) : Byte, Short, Int, Long, Float, Double = 8:16:32:64: o 4 = shows proximately 1:2:4 as: 8:4:8;

    • Long followed by “L”,Float followed by “f” or “f”;

    • In hexadecimal format :0x3FA; Base 2:0B00000011;

    • You can use underscores to make it easy to read;

    • Smaller types cannot be implicitly converted to larger types, only explicitly;

            - toByte(): Byte
            - toShort(): Short
            - toInt(): Int
            - toLong(): Long
            - toFloat(): Float
            - toDouble(): Double
            - toChar(): Char
      Copy the code
    • Data comparison judgment, involving packing.

      When nullable references are required, numbers and characters are boxed. As follows:

      val a: Int = 10000 val boxedA: Int? = a val anotherBoxedA: Int? = a print(boxedA === anotherBoxedA) //false, print(boxedA == anotherBoxedA) //true, equalityCopy the code

      Instance click here to

    • An operator

      - SHL (bits) - Signed left shift (Java <<) - SHR (bits) - Signed right shift (Java >>) - UShr (bits) - unsigned right shift (Java >>>) - and(bits) - and - Or (bits) - bit or -xor (bits) - bit xOR or -inv () - bit Non-For example, val x = (1 SHL 2) and 0x000FF000 is similar to x = (1 << 2) & 0x000FF000Copy the code
  • ToInt ()** is explicitly converted toInt

    - Character literals are enclosed in single quotes: '1'. Special characters can be escaped with backslashes. - Support these escape sequences: \t, \b, \n, \r, \', \", \\ and \$. - Encode other characters using Unicode escape sequence syntax: '\uFF00'.Copy the code
  • Boolean

  • Array – Create and assign (without deformation)

    • The library function arrayOf() creates an array and passes the element values to it

      Val asc = arrayOf (1, 2, 3, 4, 5); / / [1, 2, 3, 4, 5]Copy the code
    • The library function arrayOfNulls() can be used to create an array of a specified size with empty elements.

    • Array structure

      / / create a Array < String > initialized to [" 0 ", "1", "4", "9", "16"] val asc = Array (5, {I -> (I * I).toString()}) // - accepts array size // - factory function, which can return the initial value of each element of the given indexCopy the code
      • Other unboxed arrays :ByteArray, ShortArray, IntArray, etc.
  • string

    • Native string: A string wrapped in triple quotes and without an escape sequence of characters

      Val text = "" |" "what is this | | this is native string seems quite interesting, front what's this? Boundary prefix "" |". TrimMargin () / / | prefix is used as the default boundaries, but you can choose to other characters and passed as a parameter to / / such as trimMargin (" > ").Copy the code
    • Escape strings: strings that can have escape sequences.\t, \b, \n, \r, ‘, “, \ and $.

    • Strings use ‘$’ to refer to values to be added to the string or to call values and expressions (using “{” and “}”). ${‘$’} is required in native strings.

        val s = "abc"
        val str = "$s.length is ${s.length}" // str = "abc.length is 3"
      Copy the code

Common classes and interfaces

Simple class and interface definitions

Class ClazzName{} class ClazzName // No class body interface Foo{fun a() fun b(){... } } class A:Foo{... }Copy the code
  1. Classes can be nested with each other, can inherit or implement interfaces, and use:The operator;
  2. Interface defaultopenKey words;
  3. Interface functions can have function bodies.

This article is from Kotlin Language Center