1.1. What is Kotlin
A new programming language for the Java platform
Kotlin is clean, secure, and pragmatic, with a focus on Java code interoperability
Kotlin is a statically typed programming language that runs on a Java virtual machine. Known as the Swift of the Android world, it was designed and developed by JetBrains and is open source
In Google I/O 2017, Google announced Kotlin as the official Android development language
1.2 Main Features of Kotlin
1. Target platform: the server, Android, and wherever Java runs
-
Kotlin’s primary goal is to provide a simpler, more efficient, and safer alternative to Java that is applicable to all environments that use Java today
-
The most common application scenarios for Kotlin are
- Writing server-side code (typically the back end of a Web application)
- Create mobile applications that run on Android devices
- You can also run Kotlin code on IOS devices using Intel Multi-OS Engine
- You can also use Kotlin with TornadoFX and JavaFX to build a desktop application
- Kotlin can also be compiled into JavaScript, allowing you to run Kotlin code in your browser
2. Statically typed programming languages
-
The difference between
-
Statically typed programming language
- Check the data type at compile time, that is, write the program to declare the data type of all variables
- Java, C, C++, Kotlin
-
Dynamically typed programming language
- Data type checking at runtime means that you don’t need to declare the data type of a variable when you write the program, it will save the data type of the variable when you assign the value
- Python
-
-
Kotlin, like Java, is a statically typed programming language
-
This means that the type of all expressions is determined by the compiler, and the compiler can verify that the object contains the method or field you want to access
-
This is in stark contrast to dynamically typed programming languages. Dynamically typed programming languages represented on the JVM include Groovy and JRuby, which allow you to define variables that can store any data type, or functions that return any data type, and parse method and field references at runtime. This reduces the amount of code and increases the flexibility of creating data structures, but it has the disadvantage that problems such as misspelled names cannot be found by the compiler, leading to runtime errors
-
On the other hand, unlike Java, Kotlin does not require you to explicitly declare the type of each variable in the source code. In many cases, the type of the variable can be automatically determined based on the context, so that the type declaration can be omitted
-
Here’s the simplest example
- val x=1
-
When the variable is declared, Kotlin automatically determines that it is of type Int because the variable is initialized to an integer value. The compiler’s ability to infer the type of a variable from context is called type inference
-
-
Benefits of static typing
-
Sex can
-
Method calls are faster
- It does not need to be determined at runtime which method is being called
-
-
reliability
- The compiler verifies the correctness of the program, so the probability of a crash at run time is lower
-
maintainability
- Unfamiliar code is easier to maintain because you can see what types of objects are used in the code
-
Tool support
- Static typing enables the IDE to provide reliable refactoring, accurate code completion, and other features
-
Functional and object-oriented
-
Core concepts of functional programming
-
① first class function
- Use a function (a small piece of behavior) as a value, either by storing it as a variable, passing it as an argument, or as the return value of another function
-
②, immutability
- Use immutable objects, which guarantee that their state cannot change after they are created
-
③ No side effects
- I’m using pure functions. This type of function produces the same result for the same input, does not modify the state of other objects, and does not interact with the outside world.
-
-
Benefits of functional programming style code
-
benefit
-
(1), concise
-
The functional style of code is more elegant and concise than the corresponding imperative style of code, because using functions as values gives you more abstract power and avoids duplication of code.
- Suppose you have two pieces of similar code doing similar tasks (for example, finding a matching element in a collection) but with slightly different details (how to tell if an element is a match). You can easily extract the common parts of this logic into a function and pass different parts to it as arguments. The arguments themselves are functions, but you can use a neat syntax for these anonymous functions called lambda expressions
-
-
(2) Multi-threaded safety
- One of the biggest sources of error in multithreaded programs is modifying the same data on different threads without proper synchronization. If you use immutable data structures and pure functions, you can guarantee that such unsafe changes will not occur at all, and there is no need to design complex synchronization schemes for them
-
③ Testing is easier
- Functional programming means testing is easier. Functions without side effects can be tested independently because there is no need to write a lot of setup code to construct the entire environment on which they depend
-
-
-
The functional coding style supported by Kotlin
-
Function types
- Allows functions to take other functions as arguments, or to return other functions
-
Lambda expressions
- Allows you to easily pass blocks of code with minimal boilerplate code
-
Data classes
- Provides concise syntax for creating immutable objects
-
The standard library
- The standard library includes a rich collection of apis that let you manipulate objects and collections in a functional programming style
-
Support for nullable types
- It allows you to write more reliable programs by detecting possible null pointer exceptions at compile time
5. Free and open source
1.3 Kotlin application
The two main areas Kotlin uses are server-side and Android development
Kotlin on the server
-
The concept of server-side programming, including application types
- Returns an HTML page to the browser’s Web application
- Mobile application backend services that expose the Json Api through Http
- Microservices that communicate with each other through the RPC protocol
-
Kotlin advantage
-
Seamless interoperability with Java code
-
Kotlin’s support for the builder pattern allows you to create any object graph using a more concise syntax
- One of the simplest use cases is the HTML generation library, which can replace an external template language with a concise and fully type-safe solution
-
Persistence framework
-
The Kotlin on Android
- Make the Android development experience efficient and enjoyable
- Better application reliability
1.4 Kotlin’s design philosophy
When we talk about Kotiin, we like to say it’s a class
Pragmatic, concise, and secure language with a focus on interoperability
-
(1), the practical
-
(2), concise
-
(3), security
-
Running on the JVM already provides a number of security guarantees
- Memory security prevents buffer overflows and other problems caused by incorrect dynamic memory allocation
-
The application is type-safe, not all type declarations need to be specified, and in many cases the compiler will automatically infer the type
-
Support for empty types
-
Provides a number of convenient ways to work with controllable data
-
Helps avoid the exception type ClassCastException
- The checks and conversions in Kotlin are combined into one operation: once the type is checked, members belonging to that type can be directly referenced without additional conversions
-
-
④ Interoperability
- Java code can also easily call Kotlin’s code
- Make the most of existing Java libraries
Use the Kotlin tool
Compile the Kotlin code
-
Kotlin source code stored in the. Kt file suffix
-
The compiler analyzes the source code to generate.class files
-
Compile the code using the kotlinc command
- kotlinc -include-runtime -d java Jar
-
A brief description of the Kotlin build process
The development tools
-
LntelliJ IDEA and Android Studio plugin
-
Online playground
-
An interactive shell
- Compile small pieces of code using the REPL
- If you want to quickly try small pieces of Kotiin code, you can use an interactive shell(also called a REPL). You can enter Kotlin code line by line in the REPL and see the results immediately. You can start the REPL with the kotlinc command with no arguments. You can also launch the REPL by selecting from the “Kotlin” menu of IntelliJIDEA
-
The Eclipse plug-in