Basic Types
In Kotlin, everything is an object in the sense that we can call member functions and properties on any variable. Some types are built-in, because their implementation is optimized, but to the user they look like ordinary classes.
Numbers
Kotlin handles numbers in a way close to Java, but not exactly the same.
Kotlin provides the following built-in types representing numbers (this is close to Java):
Note that characters are not numbers in Kotlin.
Characters
Characters are represented by the type Char. They can not be treated directly as numbers
fun check(c: Char) {
if (c == 1) { // ERROR: incompatible types
// ...
}
}
Booleans
The type Boolean represents booleans, and has two values: true and false.
Arrays
Arrays in Kotlin are represented by the Arrayclass, that has get and set functions (that turn into [] by operator overloading conventions), and size property, along with a few other useful member functions:
class Array private constructor() {
val size: Int
operator fun get(index: Int): T
operator fun set(index: Int, value: T): Unit
operator fun iterator(): Iterator
// ...
}
Strings
Strings are represented by the type String. Strings are immutable. Elements of a string are characters that can be accessed by the indexing operation: s[i]. A string can be iterated over with a for-loop:
for (c in str) {
println(c)
}
Kotlin basic type:- Geniusofstudent
Reviewed by Network security
on
June 13, 2019
Rating:

No comments: