Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

๐Ÿ‘‰ About the author

As we all know, life is a long process of constantly overcoming difficulties and reflecting on progress. In this process, there will be a lot of questions and thoughts about life, so I decided to share my thoughts, experiences and stories to find resonance!!

Focus on Android/Unity and various game development tips, as well as various resource sharing (websites, tools, materials, source code, games, etc.)

Welcome to pay attention to the public account [Mr. Empty name] for more resources and communication!

๐Ÿ‘‰ premise

The current environment

2020.3.1 Patch 2 version was last downloaded on October 8, 2021

๐Ÿ‘‰ Practice

๐Ÿ˜œ Type conversion

ValueOf and Integer.valueOf are no longer available. Instead, they are in the form of to. Such as:

ย  var Temp="1234"ย ย ย ย ย ย ย  var TempOne=Temp.toInt()
Copy the code
  • ToByte (): Byte, converted to bytes
  • ToShort (): Short
  • ToInt (): Int to an integer
  • ToLong (): Long
  • ToFloat (): Float, converted toFloat
  • ToDouble (): Double, converted to a Double float
  • ToChar (): Char, converted to a character

๐Ÿ˜œ Function definition

The default definition is that the [fun] keyword indicates a function definition

private fun methon(height: Int): Int { // All types come after: (note the colon), and variables come before
ย ย ย  return height+1;
}
Copy the code

Alternatively, the function can be assigned directly to [=]

private fun methon(height: Int): Int = height + 1;
Copy the code

In addition to the above, there is one more thing: the return of the function supports the judgment statement operation, as follows

funย  methonTwo(height:Int):String{
    returnย  if (height>10) {"Greater than 10 go if"
    }else{
        "ๅฆๅˆ™่ตฐelse"}}Copy the code

And like I just said, assignments can also be combined with judgment forms,

private fun methon(height: Int): String = if (height > 10) {
    "Greater than 10 go if"
} else {
    "ๅฆๅˆ™่ตฐelse"
};
Copy the code

๐Ÿ˜œ Anonymous function

val stringLengthFunc: (String) -> Int = { input ->
ย ย ย ย ย ย ย  input.length
}
Copy the code

The above example is an anonymous function that takes a String as an argument and returns the length of the String as an int.

val stringLength: Int = stringLengthFunc("I Love Android")
Copy the code

Note that anonymous input is arbitrary, and you can change it to a different name than the system’s specific keyword

๐Ÿ˜œ variable parameter

In Java it is… Symbol (note the three dots), Kotlin is the vararg keyword

That is, an indefinite number of parameters can be used in a method definition, and a different number of parameters can be used for the same method.

The interesting thing is that in Java it’s mutable arguments, but it’s mutable numbers, and the types have to be consistent. (If you know that types can be mutable, please browse the discussion.)

Kotlin supports not only numbers, but also different data types.

If an Object is used as a variable parameter in Java, it can be called with multiple types.

๐Ÿ‘‰ other

๐Ÿ“ข author: Kom and Kom in Kom

๐Ÿ“ข reprint instructions – be sure to specify the source: Zhim Granular’s personal home page – column – Nuggets (juejin. Cn)

๐Ÿ“ข welcome to like ๐Ÿ‘ collect ๐ŸŒŸ message ๐Ÿ“