Android Development Interview: Kotlin Interview Knowledge Answers

Table of contents

Kotlin


Kotlin

  1. What is Kotlin: goals (better Java), programming methods (object-oriented imperative programming + functional programming (Scala includes macros)), some improvements (singleton mode, data data class, NPE, mutability, more syntactic sugar, type deduction)
  2. Why use Kotlin: a. Kotlin brings many features such as null safety, extension functions, coroutines, and syntactic sugar, which reduces the amount of code and makes it easier to read; b. Google has been vigorously promoting Kotlin. At present, many source codes are Kotlin , and can seamlessly switch with Java interoperability, continuing to do Android development requires us to master this language; c, I also prefer to contact new things, take the initiative to buy books Kotlin core programming
  3. var and val: definition (var is a varible variable; val is varible+final or value (value), the declared variable is a read-only variable, the reference cannot be changed, but the referenced object can be changed), Kotlin development principles (preferred to use val to Declare variables, use val, immutable objects and pure functions (that is, functions without side effects) to design programs as much as possible, with referential transparency), the advantages of using val to declare variables (a, this is a defensive coding thinking mode, It is safer and more reliable, because the variable value will not be modified (except for reflection technology); b, immutable variables make the program easier to reason about, the more complex the business logic, the greater the advantage), the advantage of using var to declare variables (a, relative Compared with other implementation methods, var has better performance and takes up less memory, which can make the program appear direct and easy to understand; b. For data structures, which may need to store a large amount of data, var is more suitable)
  4. What is a closure: A closure is a function that accesses external environment variables. a. Closures are functions; b. Closures can access external environment variables and modify them; c. Lambda expressions, anonymous functions, and function conditional statements inside functions can all be understood as closures
  5. Let, with, run, apply, and also function differences: let (applicable to uniformly judge a nullable object, the return value is the last line of the function block or specify a return expression, such as item?.let { it -> } ), with (when calling multiple methods of a class name, this can be omitted, and the return value is the last line of the function block or a specified return expression, such as with(item){this.}), run (the combination of let and with so Applicable to any scenario of let and with, the return value is the last line of the function block or specify a return expression, such as item?.let { it -> }?: run {}), apply (applicable to any scenario of run, for Initialize the object and then return the object. Multiple function chain calls can also be used, returning this, such as View.inflate(activity, R.layout.*, null).apply{}), also (applicable to any scene of let, use For multiple function chain calls, return this, such as item.also { it -> })
  6. The role of the @JvmOverloads annotation: In methods with default parameter values, using this annotation Kotlin will expose multiple overloaded methods. If this annotation is not added, only one method will use the default value for the parameter that is not passed in.
  7. Coroutines and threads: difference (a, coroutines are lightweight threads, user-mode, without operating system support; b, threads are preemptive, and coroutines are cooperative and more efficient than preemptive), coroutine advantages (light Quantity and efficiency, it does not matter to open thousands of coroutines in the thread; simple and easy to use, simplify asynchronous concurrent tasks, and solve the Java callback hell problem)
  8. Any, Object and Unit, void: Kotlin's Any is a trade-off design compatible with Java. The Java reference type is regarded as a platform type, which can be regarded as nullable or non-nullable; the introduction of Unit is to replace Java. void keyword


Android development interview series articles:


Guess you like

Origin blog.csdn.net/Agg_bin/article/details/129502783