Talking about Kotlin (2): basic types, basic syntax, and code style; talking about kotlin
Kotlin (1): Introduction and configuration in Android Studio
Through the above article, we can already program Kotlin in Android Studio. Next we will start to learn the basic types and syntax of Kotlin.
I. Basic Types
In Kotlin, the member methods and attributes of all variables are an object.
Some types are built-in, and their implementations are optimized, but they are just like common classes for users.
Note: The first letter is in uppercase and the Kotlin is case sensitive.
It is mainly a constant of the following nominal values:
-- Number type:123-- Increase the write capacity for long integers.L:123L-- Hexadecimal:0x0f-- Binary:0b00001011
Note that the octal format is not supported.
Kotlin also supports the traditional floating point representation:
-- Default Doubles:123.5,123.5e10-- Floats to addfOrF: 123.5f
Ii. Basic syntax
We know what basic types Kotlin supports. The following describes the basic syntax.
1. Two Methods for declaring an attribute in Kotlin Definition
· Var: Variable
· Val: Constant
Note: either a constant (val) or a variable (var) must be specified.
Syntax for declaring variables/constants: var age: Int = 1;
Val name: String = "Alice ";
Constant (val)/variable (var) attribute name: TYPE = initial value;
Kotlin also provides a way to write var age = 1;
Removed: Int indicates the type of the attribute and can be compiled. This is the feature of Kotlin and automatically exports the type of the value.
No error is reported.
①. Syntax of variable var
It is strange that variables in java do not require an initial value. Let's take a look at what will happen if a variable is not initialized in Kotlin.
The result is an error: the system prompts you to add an initial value.
After selecting "Add initializer", the system will find that the initial value is 0.
Take a look at the initial values given by the above six basic types of systems:
②. Constant val Syntax:
When declaring a constant, you also need to provide an initial value like the variable, and this value cannot be modified afterwards.
As shown above, a constant age of the Int type is defined and the value is 1.
In the subsequent code, assign values to the age constant, prompting that the constant age is used as the variable type. Val age: Int = 1 --> var age: Int = 1;
In summary, the val declaration constant is the same as the final statement in java. The var declaration variable must be given an initial value.
2. Kotlin annotation specifications
Consistent with java
① Single line comment
② Block-level comments
3. kotlin defines a method/Function
Basic Format:
Fun method name (Parameter Name: type, parameter name: type...): return type {
}
For example, define a method, input two types of Int parameters, calculate the sum of the parameters, and return the result of the Int type.
override fun onCreate(savedInstanceState: Bundle?) {
Super. onCreate (savedInstanceState)
SetContentView (R. layout. activity_ktest)
Var result: Int = sum (10, 20 );
}
/**
* Calculate the sum of two Int types and return
*/
Fun sum (a: Int, B: Int): Int {
Return a + B
}
Note: you do not need to add var to parameters in the method.
As you can see, you should also create basic methods. But there is a doubt that if a method does not require a return value, it is similar to void in Java
Kitlin is processed using Unit. As you can see, the sum method has no return value and is incorrectly written during the call. You cannot convert a method without return value to a certain type.
The correct syntax is as follows:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_ktest)
var a = sumA(10,20) ;
}
/*** Calculate the sum of two Int types and return */
Fun sum (a: Int, B: Int): Unit {
// Do not process
}
// Unit can be omitted if no return value exists.
Fun sumA (a: Int, B: Int ){
// Do not process
}
Iii. Code style
1. Naming Style
-- Use the camel naming method (avoid underline in naming)
-- Type name is capitalized
-- Lowercase letters of methods and attributes
-- Four spaces are used for indentation.
-- The public method must be written in the instruction document so that it can appear in the Kotllin Doc.
2. Unit
If the function returns Unit, the return type should be omitted: