Comparison of basic data types between Kotlin and Java
basic data type name | Data types in Kotlin | Java data types |
---|---|---|
Integer | Int | int and Integer |
long integer | Long | long and long |
floating point | Float | float和Float |
double precision | Double | double和Double |
boolean | Boolean | boolean和Boolean |
character type | Char | char |
string | String | String |
It looks familiar doesn't it, Kotlin turns out to be this simple. But if you type out the code for the variable declaration right away, you'll find that there's a problem with the compilation. For example, to declare the simplest integer variable, it is written as follows in Java:
int i = 0;
If you write Kotlin code according to the rules of Java, it is the following line of code:
Int i = 0;
However, Android Studio immediately prompted that the compilation failed, and it fell into the pit at the beginning of learning Kotlin. It seems that Kotlin should be taken seriously, and it cannot be easily deceived. The correct Kotlin code to declare a variable looks like this:
var i:Int = 0
The preceding var indicates that a variable declaration statement is followed, followed by a declaration in the format of "variable name: variable type", rather than the common "variable type variable name" format.. As for the semicolon that follows, it depends on whether there are other statements after the line of code. If the variable declaration is completed directly with carriage return and line feed, there is no need for a semicolon; if there is no carriage return and line feed, but other statements are added, then the variable declaration statement To put a semicolon.
Notice that the first line of variable declarations in the above type conversion code starts with val, while the rest of the variable declarations all start with var, why?In fact, the difference between val and var is that the variable modified by the former can only be assigned a value when it is first declared, and cannot be assigned any more later; while the variable modified by the latter can be assigned at any time. If it is convenient to remember, you can think of val as the final keyword in Java; as for var, there is no corresponding keyword in Java, just treat it as a routine.