kotlin variable declaration

Kotlin is a strongly typed language. Kotlin requires that all variables must be declared first and then used. When declaring a variable, the type of the variable must be explicitly or implicitly specified (implicit means that it is initialized at the same time when it is declared, so that it can be compiled when it is compiled). The type of the variable is inferred, and the new Java version also adds the var keyword).

 

Declare variables using the var, val keywords, as follows:

var | val variablename[:type] [=initial value]

 The difference with Java is that the variable type is written after the variable name, similar to the go language.

 

There are two keywords:

1. var: declares a mutable variable

2. val: declares an immutable variable (immutable here only means that it can only be assigned once. This assignment process can occur when the variable is declared, or after the declaration, and its value cannot be modified after the assignment)

 

In the above syntax, either explicitly specify the type of the variable in the form of ":type", or specify an initial value for the variable -- the Kotlin compiler will determine the type of the variable based on the initial value, and neither can be declared when a variable is declared. Specifies the variable type and also does not specify an initial value.

But we can also specify the type and specify the initial value at the same time, but the variable type at this time must be the same as the type inferred by the initialization type.

 

example:

fun main(args: Array<String>) {
    // Specify both type and initial value when declaring a variable
    var a: Int = 5
    val a1 :Int = 5
    
    // Declare the variable with the specified type, without specifying the initial value
    var b: Int
    b = 6
    val b1: Int
    b1 = 6
    // b1 = 8 // error, val variable cannot be repeatedly assigned
    
    // The declared type does not specify the type, the type is inferred from the type
    var c = 7
    val c1 = 7
}

  

An immutable variable declared with val is actually a constant, which means that once its value is initialized, it cannot be reassigned. There are two types of constants in Kotlin depending on where the output is located.

1. Local scope constant: This kind of constant allows no initial value to be specified at the time of declaration, as long as the initial value is specified before the first use.

2. The constant attribute of the class: This kind of constant attribute can either specify the initial value at the time of declaration, or specify the initial value in the constructor of the class or structure.

 

It should be pointed out that since the bytecode compiled by the Kotlin program must comply with the JVM specification, if you define variables and functions directly in the Kotlin program, kotlinc will automatically generate a class named "file name capital + Kt" , and convert the variable to the static getter and setter methods of the class (where val declares only the getter method), and the function is converted to the static method of the class.

 

Another point to note is that since kotlinc will generate additional classes for Kotlin programs that contain functions and variables, it is required that classes with the same name cannot be repeatedly defined under this package. For example, if we define a Kotlin program named liang.kt, and the program contains functions or variables, then kotlinc will automatically generate the LiangKt class, so the LiangKt class cannot be repeatedly defined under this package.

 

 

Integer

Similar to Java, Kotlin also provides 4 integer types.

Byte: Occupies 8-bit memory, the range is -128~127. Compatible with Java's byte and Byte types.

Short: Occupies 16-bit memory, the range is -32768~32767. Compatible with Java's short and Short types.

Int: occupies 32-bit memory, the range is -2147483648~2147483647. Compatible with Java's int and Integer types.

Long: Occupies 64-bit memory, ranging from -2 63 to 2 63 -1. Compatible with Java's long and Long types.

Since Int is the most commonly used integer type in Kotlin, if you declare a constant or variable without specifying a data type, but simply specify its initial value as an integer, then Kotlin will automatically determine the type of the variable as Int.

 

Kotlin's integer type is different from Java's. Kotlin's integer type is not a basic type, but a reference type (roughly equivalent to Java's wrapper class). Byte, Short, Int, and Long all inherit the Number type, so they can call methods, Access properties.

 

One thing that needs to be explained in advance is that Kotlin is a null-safe language, so Byte, Short, Int, Long type variables cannot accept null values. If you want to store null values, you should use Byte?, Short?, Int?, Long ? Types of. Such as:

// Variables of type Int do not support null values, so the following code is wrong
var notNull: Int = null
// Int? is equivalent to an Int that supports null values, so the following code is correct
var nullable: Int? = null

  

It can be seen that the Kotlin language allows adding "?" after the existing data type. The data type after adding "?" is equivalent to extending the original type, and the data type with "?" can support being assigned a null value.

 

In addition, there is a difference between adding a "?" suffix to an integer type and not adding it -- an integer variable of a normal type will be mapped to a Java basic type; an integer variable with a "?" suffix will be mapped to a basic type. wrapper class. Such as;

var pm1: Int = 200 // pm1's type is Java's int type
var pm2: Int = 200 // pm2's type is Java's int type
println(pm1 === pm2) // Basic type comparison, output true

var obj1: Int? = 200 // The type of pm1 is Java's Integer type
var obj2: Int? = 200 // The type of pm2 is Java's Integer type
println(obj1 === obj2) // reference type comparison, output false

  

There are three representations of Kotlin integer values:

1. Decimal

2. Binary: Integer values ​​starting with 0b or 0B are binary integers

3. Hexadecimal: Integer values ​​starting with 0x or 0X are hexadecimal integers

 

floating point

There are two types of floating point types in Kotlin

1. Float: Represents a 32-bit floating point type, which can be used when the precision requirement is not high

2. Double: It represents a 64-bit double-precision floating-point type. This type is used when the program needs to store very large or high-precision floating-point numbers.

 

There are two representations of floating point numbers in Kotlin.

Decimal form: This form is simply a floating point number, such as 5.12, 3.2, etc. Floating point numbers must contain a decimal point, otherwise they will be treated as integer types

Scientific notation: eg 5.12e2, 5.12E2, etc.

It should be pointed out that only floating-point values ​​can be represented in scientific notation.

 

If you declare a constant or variable without specifying a data type, but only specify that its initial value is a floating-point number, then Kotlin will automatically determine that the type of the variable is Double.

 

In addition to this, Kotlin provides 3 special floating-point numbers: positive infinity, negative infinity, and not-a-number. For example, dividing a positive number by 0.0 will result in a positive infinity number, using a negative number dividing by 0.0 will result in a negative infinity number, and dividing 0.0 by 0.0 or taking the square root of a negative number will result in a non-number.

It should be pointed out that all positive infinity numbers are equal, and all negative infinity numbers are equal; non-numbers are not equal to any number, not even non-numbers themselves.

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325020557&siteId=291194637