Study site

1. Kotlin website

2. Google learned the Kotlin programming language

3. GitHub from-java-to-kotlin

4. Rookie tutorial Kotlin

The article

1. Several ways to create singletons using Kotlin

[5 kinds of singleton pattern under the Kotlin] (https://www.jianshu.com/p/5797b3d0ebd0)Copy the code

2. Kotlin create variable, constant, static constant method

[kotlin static constants declared] (https://blog.csdn.net/yinianjian2017/article/details/80771536) [kotlin practical guide 2: Variables, constants, and the static constants] (https://blog.csdn.net/zhaoyanjun6/article/details/87811333)Copy the code

3. Method annotations in Kotlin do not have parameters, so use BugKotlinDocument plug-in to solve the problem.

practice

1. Web-based interactive code editor where you can practice writing Kotlin programs

Developer. The android. Google. Cn/training/ko…

2. Kotlin’s progress:

developer.android.google.cn/kotlin

+

1. I am currently watching the basic course: Android basics in Kotlin (Developer. The android. Google. Cn/kotlin/android…)
2: See later: Kotlin Boot Camp for programmers (Developer.android.com/courses/kot…)
3: Android Kotlin basicsDeveloper.android.com/courses/kot…)
4: Later: Advanced Android development with Kotlin (Developer. The android. Google. Cn/courses/load…)
Specific progress of 1:

Developer. The android. Google. Cn/courses/and… + developer. The android. Google. Cn/courses/and… + Add a button to an app (google.cn)

knowledge

1. The variable declared by val can be assigned only once;

2. The var keyword declares a variable that can be changed;

3. Use variables in output statements: ${variable name}

var age = 365 * 8
println("You are already ${age}")
println("${age} is the very best age to celebrate!")
Copy the code

4. Repeat create loop statement: repeat(loop times){}

repeat(10){
    print("Silly dog")}Copy the code

5. Run Hello World with IntelliJ IDEA:www.web3.xin/code/2230.h…

  • The mistake I made at the beginning was to create a Kotlin class and write main inside it. The run/ run button cannot be found.
  • You should create a Kotlin file, not a class, and write the main function in it, and you can run it

6. IntRange: Used to represent a range of Int values. From A to B.

var i1 = 1.6.
var i2:IntRange = 1.6.

fun showRandom(a){
    println(i1.random())
    println(i2.random())
    // We can create an IntRange instance directly
    println((1.6.).random())
}
Copy the code

7. If a class is to be inherited, it can be decorated with the open keyword

open class BaseActivity : AppCompatActivity() {}

class Jump : BaseActivity() {}
Copy the code

8. Kotlin and Java code transfer

  1. Java file to Kotlin file
    • Code -> Convert Java File to Kotlin File
  2. Kotlin file to Java file
    • Tools -> Kotlin -> Show Kotlin ByteCode
    • Decompile

9

  1. An instance of a Class in Java is written in Kotlin as ClassName::class.java
  2. Class instances as method arguments are written clazz:Class<*>
fun jump(clazz: Class< * >) {
    val intent = Intent(this, clazz)
    startActivity(intent)
}

fun J1(view: View) {
    val clazz = MainActivity::class.java
    jump(clazz)
}
Copy the code

10. Static functions

With this annotation (@jVMStatic), the internal functions and properties of the Object and Companion Object are truly static code
open class T {
    companion object{
        @JvmStatic
        open fun f1(a):Unit{}}}Copy the code

11. How to define static constants

Const val life static constant in object
object ObjectTest {
    const val a1:String = "111"
    open fun f1(a):Unit{}}Copy the code
// Compile to Java code
public final class ObjectTest {
   @NotNull
   public static final String a1 = "111";
   @NotNull
   public static final ObjectTest INSTANCE;

   private ObjectTest(a) {}static {
      ObjectTest var0 = newObjectTest(); INSTANCE = var0; }}Copy the code

12. How to define a static function

Kotlin implements static methods and static variables in two ways
object ObjectTest {
    @JvmStatic
    open fun f1(a):Unit{}}Copy the code
// Compile to Java code
public final class ObjectTest {
   @NotNull
   public static final ObjectTest INSTANCE;
   @JvmStatic
   public static void f1(a) {}private ObjectTest(a) {}static {
      ObjectTest var0 = newObjectTest(); INSTANCE = var0; }}Copy the code

13. Elvis operator: Does it pass? To simplify the if null operation

? : Specifies the default value when the attribute is null, return, throw 1 exception
vardate = lesson? .date ? :"Monday"
vardate = lesson? .date ? :return
vardate = lesson? .date ? :throw new Exception("* * *")
Copy the code

14. Bit operators

Kotlin operator.

Kotlin also supports the following seven bitwise operators.

  1. And: indicates the bitwise and. Returns 1 when both digits are 1.
  2. Or: bitwise or. If one of them is 1, you can return 1.
  3. Inv: indicates bitwise non. Unary operator that inverts every bit of the operand, including the sign bit.
  4. Xor: xOR by bit. Returns 0 if two digits are the same, 1 if not.
  5. SHL: left-shift operator.
  6. SHR: right shift operator.
  7. Ushr: Unsigned right-shift operator.

Kotlin’s bitwise operators work only on Int and Long.

First of all, the difference between left shift and right shift is well distinguished. Left shift << : that is, the number corresponds to the binary code as a whole to the left, the excess part of the left is discarded, and the right is filled with zeros. For example, the binary code of 253, 1111 1101, is 1111 0100 after 253<<2. Very simple right shift >> : The binary code corresponding to the number is moved to the right as a whole, the left part is supplemented with the original flag bits, and the excess part on the right is discarded. Unsigned right shift >>> : Regardless of whether the positive and negative flag bits are 0 or 1, the binary code of the number is moved to the right as a whole. The left part is always filled with 0 and the right part is discarded. For example, 5 indicates 1111 1011 in binary. The red digit indicates the binary digit. 5>> 2:1111 1011-------------->1111 1110. 11 for the original sign a supplement to get 1-5 > > > 2:1011-1111 -- -- -- -- -- -- -- -- -- -- -- -- -- > 0011, 1110. The left-hand side is always filled with 0 to complement 00Copy the code

15. Cycle control

Kotlin cycle control
for (int i = 1; i <= 10 ; i++) { }

for (int i = 1; i < 10 ; i++) { }

for (int i = 10; i >= 0 ; i--) { }

for (int i = 1; i <= 10 ; i+=2) {}for (int i = 10; i >= 0 ; i-=2) {}for (String item : collection) { }

for (Map.Entry<String, String> entry: map.entrySet()) { }

Copy the code
for (i in 1.10.) {}for (i in 1 until 10) {}for (i in 10 downTo 0) {}for (i in 1.10. step 2) {}for (i in 10 downTo 0 step 2) {}for (item in collection) { }

for ((key, value) in map) { }
Copy the code