Kotlin basic data types and data operations:
- Basic numeric type
- Floating point types
- Character types
- String type
- Type cast
- Digital operation
I. Basic data types:
Kotlin’s basic numeric types include Byte, Short, Int, Long, Float, Double, and so on. Unlike Java, a character is not a numeric type, but a separate data type.
type | A wide | The minimum value | The maximum |
---|---|---|---|
Byte | 8 | – 128. | 127 |
short | 16 | – 32768. | – 32767. |
Int | 32 | – 2147483648 (2 ^ 31) | (2^ 31-1) |
Long | 64 | – 9223372036854775808-2 ^ (63) | 9223372036854775807 (2 ^ 63-1) |
1. Integer types:
val number = 100 // The default is Int
val bigNumber = 8000000000 // The maximum value beyond Int is of type Long
val longNumber = 20L // The number is followed by an L to declare type Long
val byteNumber:Byte = 1
Copy the code
Ps: all variables initialized with an integer value that does not exceed the maximum value of an Int default to Int. If the initial value exceeds its maximum value, it is inferred to be of type Long. Adding L explicitly after a numeric value indicates a Long type
2.Float, Double:
Kotlin provides two types, Float and Double, to represent single-precision and double-precision floating-point types, respectively.
type | A wide |
---|---|
Float | 32 |
Double | 24 |
val doubleNumber = 31415928888 // The default is Double
val floatNumber = 31415928888f// a trailing f or f explicitly indicates that this is a Float
Copy the code
Ps: Kotlin’s default inference for decimals is of type Double. If you want to explicitly specify a decimal as a Float, add either f or f to the end of the number. Since Float has six decimal digits, the actual value of floatNumber in this example is 31415926. Another difference in Kotlin from Java is that there is no implicit broadening conversion for numbers. For example, a function that takes a Double can only accept Double, not Float, Int, or any other numeric type
3. Character types:
Characters in kotlin are represented by type Char, and their values are enclosed in single quotes
4. Boolean type:
In Kotlin, use Boolean to represent a Boolean type, which has only two values true and false. Notice the nullable type Boolean? Type will have a boxing operation.
val isVisible: Boolean = false
val isVisible = false // Automatically infer to a Boolean type
Copy the code
5. String type:
In Kotlin strings are represented as strings. Strings are immutable. Elements of a string — characters can be accessed using the index operator :s[I]. We can use the for loop to select the substitution string:
val str:"It's Saturday"
for(char in str){
println(char)
}
Copy the code
5. String template
String literals can contain template expressions, which are little pieces of code that evaluate and merge the results into the string. The template expression starts with a dollar character ($) and consists of a simple name
val number = 100000
println("The Result is $number")
Copy the code
Or any expression enclosed in curly braces ${
val text ="This is Text"
println("Text length is ${text.length}")
Copy the code
Templates are supported inside both strings and escaped strings. If you need to represent the literal $character in a raw string (which does not support backslash escape), you can use the following syntax:
val price ="${} '$' 9.99"
Copy the code
Like Java, Kotlin can concatenate strings with the + operator. This also applies to dry connect strings with other types of values.
6. string values:
Kotlin has two types of string literals: escape strings can have escape characters, and raw strings can contain newlines as well as arbitrary text. Here is an example of an escaped string:
vals="Hello, world! \n" / / \ n a newline
val s2="{\"key\":\"value"}"//\ backslash pair""Escape, preserve string formatCopy the code
Strings are enclosed by three quotation mark () delimiters, are not escaped internally and can contain newlines and any other characters
val text = """
for (c in "foo")
print(c)
Copy the code
Leading whitespace can also be removed with the trimMargin() function:
val text = """
|Tell me and I forget..
|Teach me and I remember.
|{"key1":"valuel} | "" {"key2":"value2"}""".trimMargin()
Copy the code
7. Type cast:
Unlike Java, Kotlin casts numeric types by calling functions such as toInttoDoubletoFloat.
type | Casts. |
---|---|
Byte | toByte() |
Short | toShort() |
Int | tolnt() |
Long | toLong0) |
Float | toFloat() |
Double | toDouble() |
Char | toChar) |
val number =100 // Declare an integer number object
number.tostring()
number.toByte()
number.toShort()
number.toLong()
number.toFloat()
number.toDouble()
~~~
Copy the code
Two, number operation:
A. Four operations
Division / :
val number = 3 / 2
println(number) / / output 1
val floatNumber = 3 / 2.toDouble()
println(number) / / output 1.5
Copy the code
Multiplication * :
val number = 3 * 2
println(number) / / output 6
Copy the code
Add + :
val number = 3 + 2
println(number) / / output 5
Copy the code
Subtract one:
val number = 3 - 2
println(number) / / output 1
Copy the code
Take more than % :
val number = 3 % 2
println(number) / / output 1
Copy the code
B. a operation
Unlike Java, bit-operations in Kotlin are not represented by a special notation, and can be used as infix functions to call named functions.
- SHL (bits)- Signed left shift
- SHR (bits)- Signed right shift
- Ushr (bits)- Unsigned right shift
- And (bits) – with
- – or the or (bits)
- Inv () – a non
- – an exclusive or xor (bits)
val vip= true
val admin= false
val result =vip and(admin)=false
val result=8 ushr(2) =2
Copy the code