Kotlin language basics (2) - variables and data types

Kotlin language basics-variables and data types

1. Kotlin variables

Kotlin variables have two forms: var (variable) and val (value, value).
Val defines a read-only quantity. Once created, its value will not change.
Example:

val a = 23

Then the corresponding value of a can only be 23 and will not change. Any attempt to reassign a will result in a compilation error.
var defines a mutable variable, and the value of the variable can be modified multiple times.
example:

var b = 23
b=25

Any data in the Kotlin language is an object, so objects have a specific data type.

2. Data type

1. Common data types

Short: short integer (16 bits)
Long: long integer (64 bits)
Int: integer (32 bits)
Double: double precision real number (64 bits)
Float: single precision real number (32 bits)
Byte: byte type (8 bit)
Boolean: Boolean type (value true or false)

Among the above types, common basic types are represented.
For example:

var a = 23

The compiler will automatically infer that the data type is Int

var b = 23.0

The compiler will automatically infer the data type as Double
Note: var c: Int = null is wrong. No ? after the type of any variable indicates a non-nullable type.
If it needs to be expressed as a nullable type, do you need to add it after the data type? , it is correct to define variables of c in the form:
var c:Int? = null .

2. Any and Any?
The parent class of any non-empty type is the Any type, and the Any type is expressed as any type:
for example:

var d:Any = 23
d = true
d = 23.34

The above representations are all correct.

If any type needs to be defined as a nullable type, it can be expressed as an Any? type, for example:

var e:Any? = null
e = 23
e = false

3.Nothing
The Nothing type represents no object instance. A non-existent value can be represented by Nothing. If a function returns Nothing, it means that the function did not return any value (usually an exception is thrown). example:

fun doSomething():Nothing{
    
    
    throw NullPointerException("空指针")
}

There is a special representation, such as

var c = null

At this time, the compiler will parse c into Nothing? Nullable essentially means that it does not represent any instance object, that is, it represents a non-existent instance object.

4. Related operations of nullable types
(1) Safe calling?
If an object is null, calling the method of this object will throw a null pointer exception, causing running problems. Therefore, it is necessary to avoid this situation. You can use safe calls to judge the object first. If it is empty, return null, otherwise perform the operation. For example:

var str:String? = null
println(str?.length)
str = "hello"
println(str?.length)

The running result is:

null
5

(2) Elvis operation The
Elvis operator is ?:,
which means that if the object is null, the result of the expression behind ?: is returned; otherwise, the object itself is returned.
For example:

var str :String? = null
println(str?:"空字符串”)
str = "hello"
println(str?:"空字符串”)

The running result is:

empty string
hello

(3) Non-null assertion operation
The non-null assertion operator is !!, which is used to determine whether the object is null. If so, a NullPointerException exception is thrown, otherwise subsequent operations are performed. example:

var str:String? = null
println(str!!.length)

The running result is:

Exception in thread “main” java.lang.NullPointerException …

Another example:

var str:String? = “hello”
println(str!!.length)

The running result is:

5

Reference:
Chen Yi "Android Mobile Application Development (Micro Course Edition)" Tsinghua University Press ISBN 978-7-302-59734-6

Supongo que te gusta

Origin blog.csdn.net/userhu2012/article/details/132676216
Recomendado
Clasificación