Basic types of

Var age: Int = 123 var sex: Int = 123 var sex: Int = 123 var sex: Int = 123 = null // Declare a nullable String variable sex, String and String? Not the same typeCopy the code

The String and String? Is one of two types. The former cannot modify a null value; The latter added “?” Then the modified variable can be null, which is also a manifestation of kotlin’s null-safety.

When the compiler can infer the data type of a variable, it can dispense with the colon and the following data type. For example:

Var age = 123 val name = "Tom" var age = 123 val name = "TomCopy the code

Double exclamation mark “!!” You can strongly convert the type, as shown in the following code. Is name2 an nullable String? Type, and an error will be reported if you assign it directly to a non-null name1. If you are sure that name2 is not empty, you can add a “!!” to the end. Turn better.

Var name1: String = "Tom" // Name1 cannot be empty var name2: String? = "Jack"; // Name1 = name2 // Error is reported.Copy the code

The keyword

  1. The open. Classes declared as open can be inherited. Note that a class in Kotlin is final by default, meaning that the default class cannot be inherited.


The kotlin function is preceded by the fun keyword, and the return value type is written after the input parentheses and before the function body curly braces:

Fun main() {printLen(" chestnut ")} fun printLen(STR: String): String {println(" $STR!") Return STR} fun printLen(STR: String = "I am the default value ~"): String {println(" $STR!"); Return STR} // Return STR}Copy the code

The Kotlin method can be written directly in a.kt file instead of in a class

For example, there is a file Util. Kt that contains many utility class methods. If you were in Java, you would have to write code in the class:

public class Utils { public static final void echo(String name) { println("name = " + name); }}Copy the code

When called, it is:

Utils.echo("Hello UnderWorld! ");
Copy the code

In Kotlin code, we can write directly in the Util. Kt file like this:

Fun echo(name: String) {println("name = $name")}Copy the code

Calling it in Java code can be written as follows:

Public static void Main (String[] args) {utilkt. echo("Hello World! "); }Copy the code

Intermodulation with Java code

Fun say(MSG: String) {println(MSG)}}Copy the code

Call the say method in Test from kotlin code:

Test.say("Good Morning~")
Copy the code

In Java code, then:

Test.INSTANCE.say("Good Morning~")
Copy the code

To call a Java class in Kotlin, instead of writing: test.class as in Java, write: In addition, Kotlin classes are compiled as KClass files, not class files. So, in Kotlin code, if you want to call a Kotlin class, instead of using suffix, write: Util::class.

Conflict resolution between Java and Kotlin

  1. Keyword conflict. For example, the keyword in, which is a keyword in Kotlin, needs to be backquoted to resolve this conflict if you want to refer to an object in a Java class:
'in' // In is a property in utils. Java: public static int in = 100;Copy the code
  1. Kotlin has no encapsulated classes. Kotlin does not have a wrapper class like Integer. Only basic types like Int can be called or used to identify the wrapper type of Integer only through reflection.

Here are a few examples of web applications, to be supplemented in practice. 1) Use integer.class in kotlin code. If a Java Class has a method: void func(Class clazz){}, what if Kotlin needs to pass an Integer. Class? The correct way is: func (Int: : class. JavaObjectType), rather than the func (Int: : class. Java) 2) Int: : class. Java points to the kotlin Int in the standard library. Kt. Int: : class javaObjectType point to an Integer in the JDK. Java classes. 3. Kotlin is empty safe. If Kotlin calls code in Java, it needs to use ***? To prevent null pointer exceptions. For example, if you have a String object in Java, if you want to use it in Kotlin, you need to use String, right? Type to receive. 4. Kotlin has no static variables or static methods. The problem with no static methods can be solved by adding @jvmstatic annotation before the method:

object Utils {
    fun getName(): String{
        return "hehe"
Copy the code

It is also possible to write methods in the companion Object {} of a class.

Extension function

Kotlin supports the ability to add extensions to existing classes through extension functions. We can add the methods we need for objects in the third-party library. For example, we can extend a method in the User class:

Fun user.getInfo (): String {// return uid. ToString () + name}Copy the code

In this way, we have added a method getInfo to the User class, which can then be called on objects of the User class. Note that the extension function is added to the class statically and has no runtime polymorphism. Look at the following code:

Open class Animal class Dog: Animal fun dog. name() = "Animal" Animal fun dog. name() = "Animal" Animal fun dog. name() = "Animal" Return dog fun Animal. PrintName (Animal: Println (} fun main(args: Array<String>) {Dog().printName(Dog()) // Prints "animal", indicating that the extension function is not polymorphic at run time. }Copy the code

Decompile this code into Java code, and you can see that the final call, Dog().printName(Dog()), is compiled to printName((Animal)(new Dog()), (Animal)(new Dog()); That is, the last call will forcefully convert the Dog object into an Animal object, which is no longer polymorphic.

Lambda closure

  1. Lambda closure declarations, which can be:
// lambda closure val print = {name: String -> // lambda closure val print = {name: String ->Copy the code

There is a limit to the number of arguments in the closure, up to 22. Because Kotlin only defined Function22 with 22 parameters for us, as shown in the figure below:

What if we need a Lambda closure with 23 arguments? At this point we need to manually declare Function23 in the Kotlin package. We need to manually define Function23 for a Java class, because only a kotlin library can declare a kotlin package name, and we can’t declare a kotlin package name ourselves, but Java and Kotlin are interworking. So we can declare Function23 as a Java class and set its package name to kotlin, so we can declare closures with more than 22 arguments.

package kotlin;

public interface Function23<P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, P12, P13, P14, P15, P16, P17, P18, P19, P20, P21, P22, P23, R> extends Function<R> {
    R invoke(P1 p1, P2 p2, P3 p3, P4 p4, P5 p5, P6 p6, P7 p7, P8 p8, P9 p9, P10 p10, P11 p11, P12 p12, P13 p13, P14 p14, P15 p15, P16 p16, P17 p17, P18 p18, P19 p19, P20 p20, P21 p21, P22 p22, P23 p23);
Copy the code

Higher-order functions

A characteristic of higher-order functions is that the argument to a function (Lambda) is also a function (Lambda). The default return value of a function is an object of type Unit. But if the function is taken as an argument, then the return type must be:

// the following block function is executed Onlyif isDebug is true. The block function is taken as an argument, so the return type should explicitly write fun Onlyif(isDebug: Boolean, block: () -> Unit) { if (isDebug) block() } fun main() { val runnable = Runnable { print("Runnable run!" )} val function: () -> Unit function = runnable::run // runnableCopy the code

Kotlin’s Lambda is compiled as an anonymous inner class, and methods can be modified with the inline keyword, so that when the method is compiled, calls to the method are disassembled into statement calls, thus reducing the creation of unnecessary objects. Note, however, that using the inline keyword too much adds to the compiler burden, so inline only applies to higher-order functions, such as the one above:

inline fun Onlyif(isDebug: Boolean, block: () -> Unit) {
    if (isDebug) block()
Copy the code

Classes and objects

  1. The Kotlin class is decorated by public final by default, and the default superclass is Any, not Object.
  2. The Kotlin class constructor calls the init method by default, so we can do some initialization in the init method:
class TestView: View { constructor(context: Context): super(context) constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0) constructor(context: Context, attrs: AttributeSet? , defStyleAttr: Int) : super(context, attrs, defStyleAttr) init {print(" constructor executed ~")}}Copy the code
  1. There are four types of access modifiers: private, protected, public, and internal. The first three are the same as Java. Internal means that a module can be accessed internally, but other modules cannot be accessed.
  2. Kotlin’s companion object. Static methods and static variables can be implemented:
Class StringUtils {// Companion object implements static variable val TAG = "StringUitls" // Companion object implements static method fun isEmpty(STR: String) : Boolean { return "" == str } } }Copy the code
  1. Implementation of singletons in Kotlin. A singleton of Kotlin can be implemented using a companion object:
Class SingleInstance private constructor() {companion object {fun get() SingleInstance {return holder-instance. Instance}} Private object Holder {// Create an anonymous inner class with object val instance = SingleInstance() } }Copy the code


  • Access integer.class from Kotlin
  • Kotlin calls the method written in Java with the Class parameter

Not enough? Welcome to browse my public number. Search: repair bamboo, or scan the QR code below to follow

Also welcome zhihu searchRepair of bamboo~

If you give a rose, you leave a fragrance in your hand. Welcome to retweet, share and follow, your recognition is the spiritual source of my continued creation.