Let me draw a picture before I write it





DFD16CDA-8031-4A67-9206-80004DAAF416.png

It seems that using Kotlin is really the trend, since the I/O conference I believe that everyone has more or less contact with Kotlin, Kotlin has many advantages, simple, safe, it can be used in almost any place Java use; Server development, Android application…… Kotlin works well with all existing Java libraries and frameworks, and here are some useful tips that may be overlooked in development.

————— (Java function with return value) —————

public int max(int a, int b) {

if (a > b)

return a;

return b;

}

————— (Kotlin) —————

fun max(a: Int, b: Int): Int {

return if (a > b) a else b

}

As you can see, Kotlin is already more concise than Java, but it could be even simpler:

fun max(a: Int, b: Int) = if (a > b) a else b
Copy the code

That’s it? Yeah, just one line, why not declare the return type? This type of function is called an expression body function. The compiler will parse the expression as the body and treat its type as the return type of the function, even if it is not written out. This type of analysis is called type derivation. Note here that only the return type of the expression function can be omitted.

String template

fun main() {

val name = “kotlin”

println(“hello,$name”)

}

If you declare a variable name in your code and use it in the following literal value of the string, Kotlin allows you to reference local variables in the string, as long as you prefix the variable name with $, which is equivalent to Java (” hello “+name), but much more compact! If I want to use the $character in a string, you have to escape it: println(“$x”) prints $x and does not interpret x as a reference to a variable. You can also nest double quotes directly within double quotes, as long as they are within the range of an expression (inside curly braces)

fun main(args: Array<String>) {

println(“hello,${if (args.size > 0) args[0] else “someone”}”)

}

Those of you who have used Kotlin know that the for loop in Kotlin is a little different, more like foreach Fun main(args: Array

) {for (a in args.indices){println(a)}} Use the in operator to check if a value is in an interval, for example:

fun isLetter(c: Char) = c in 'a'.. 'z' || c in 'A'.. 'Z'Copy the code

Println (isLetter(s)) true checks whether the character passed in is in or between azs.

fun isNotDigit(c: Char) = c ! in '0'.. '9'Copy the code

Println (isNotDigit(x)) true checks if the character passed in is not between 0 and 9 by adding one before in! Represents the inverse of in

In Kotlin, we imagine that using a Switch statement in Java requires a change to when: val x: 10 when Int = (x) {9 – > println (” x: ${x + 10} “) 10 – > println (” x: $x “) else – > print (” x: $x “)} is feeling the same, Fun recognize(c: Char) = when (c) {in ‘0’.. fun recognize(c: Char) {in ‘0’.. ‘9’ -> “it’s a digit” in ‘a’.. ‘z’, in ‘A’.. ‘Z’ -> “it’s a letter!” else -> “i don’t know…” } println(recognize(‘8’)) it’s a digit

Friends of the company recently looking for Android engineers to ask for 3 years of work experience above the coordinates of Beijing Wangjing intent direct MD resume to [email protected]