Kotlin basics for Java developers
Java developers can start writing handwritten code after reading the basics (plus an empty security article). And then you can use the specific points as you go along.
Basic differences with Java
- There is no
new
Keyword, directly create the object. - There is no
;
. - The value is Nullable(tape)
?
) and Non – Null. - Both inheritance classes and implementation interfaces are used
:
.
type
Data types supported in Kotlin: Double, Float, Long, Int, Short, Byte. Instead of supporting decimal-to-large conversions by default as Java does, all conversions go through explicit methods, such as the toInt() method.
The character type: Char, cannot be treated as a number as in Java. Boolean type: Boolean.
All of the above types are represented at run time as JVM native types (except for nullable and generic applications), but they have only one way to write them in code (unlike Int and Integer in Java), and they look like normal classes to the user.
All types that require nullable types are automatically boxed.
String types: String is immutable. You can use [] to access elements. You can add $to template expressions (which can be variables or expressions contained in {}).
Method statement
Method declaration format:
Fun Method name (argument): Return value type {method body}Copy the code
The parameter list starts with the variable name and ends with the variable type.
Such as:
fun sum(a: Int, b: Int): Int {
return a + b
}
Copy the code
If the body of a method returns a simple expression, you can use =:
fun sum(a: Int, b: Int) = a + b
Copy the code
In this case, the return value type can also be omitted because it can be inferred.
The return value is null: Unit can be omitted.
When a method has body, the return value type cannot be omitted unless the return value is Unit.
Methods can have default arguments
When declaring methods, you can use = to set default values for arguments. When this parameter is omitted when the method is called, the default value is used.
You can specify the name of an argument when calling a method.
fun reformat(str: String,
normalizeCase: Boolean = true,
upperCaseFirstLetter: Boolean = true,
divideByCamelHumps: Boolean = false,
wordSeparator: Char = ' ') {... }Copy the code
When called:
reformat(str, wordSeparator = '_')
Copy the code
Documents: Functions provides
Variable declarations
Variable declarations have two keywords: val and var.
Val can be assigned only once, equivalent to final.
val a: Int = 1 // immediate assignment
val b = 2 // `Int` type is inferred
val c: Int // Type required when no initializer is provided
c = 3 // deferred assignment
Copy the code
When a variable is declared, it can omit the type if the initialization can infer the type.
Process control
if
In Kotlin, if is an expression, that is, it returns a value.
Its branches can be blocks surrounded by {}, where the last expression is the value of the corresponding branch.
val max = if (a > b) {
print("Choose a")
a
} else {
print("Choose b")
b
}
Copy the code
When an if is used as an expression, it must have else.
Common ternary operator conditions in Java? Satisfaction: Dissatisfaction, which does not exist in Kotlin.
The Elvis Operator in Kotlin is used to make null judgments and then select processing (see the next null security article).
when
When is used to replace switch in Java.
when (x) {
1 -> print("x == 1"2 - >)print("x == 2")
else -> { // Note the block
print("x is neither 1 nor 2")}}Copy the code
Like if, when can be used as an expression or statement. Else is required when used as an expression (unless the compiler can infer that all cases have been overridden).
cycle
while, do… While, break, continue are all used in the same way as In Java.
The use of for is similar except in Java: in Kotlin becomes in.
Kotlin has some Range operators: Ranges
This article is available at github.com/mengdd/Kotl…
The resources
- Kotlin Basic Syntax
- Basic Types
- Functions
- Control Flow