本章我们会学习 Kotlin 语言中可用的基础数据类型。

数字

Kotlin 中的数据类型标识与 Java 中十分相似,不过,Kotlin不允许不同数据类型之间的隐式转换。以下表格列举了不同数字类型的字节大小。

Type Size
Double 64
Float 32
Long 64
Int 32
Short 16
Byte 8

在随后的例子中,我们可以看到 Kotlin 如何输出不同的数据类型。请输入以下代码来运行看看:

Live Demo

  1. fun main(args: Array<String>) {
  2. val a: Int = 10000
  3. val d: Double = 100.00
  4. val f: Float = 100.00f
  5. val l: Long = 1000000004
  6. val s: Short = 10
  7. val b: Byte = 1
  8. println("Your Int Value is "+a);
  9. println("Your Double Value is "+d);
  10. println("Your Float Value is "+f);
  11. println("Your Long Value is "+l);
  12. println("Your Short Value is "+s);
  13. println("Your Byte Value is "+b);
  14. }

输出:

  1. Your Int Value is 10000
  2. Your Double Value is 100.0
  3. Your Float Value is 100.0
  4. Your Long Value is 1000000004
  5. Your Short Value is 10
  6. Your Byte Value is 1


[

  1. ]()
  2. [
  3. ]()

字符

Kotlin 使用 char 类型来表示字符。字符通过单引号'c' 的方式来定义,字符串通过双引号如 "hello" 的方式定义。此外 Kotlin 的变量有两种方式可以声明 - 一种是使用 var 关键字另外一种是使用 val

Live Demo

  1. fun main(args: Array<String>) {
  2. val letterA: Char // 声明一个字符变量
  3. letterA = 'A' // 之后再赋值
  4. val letterB: Char = 'B' // 声明的同时赋值
  5. var greeting: String = "hello" // 声明并定义一个字符串变量
  6. var name = "Alan" // 未指定变量类型直接定义字符串
  7. println("$greeting, $name. $letterA $letterB!")
  8. }

输出:

  1. hello, Alan. A B!

Boolean

Boolean 类型跟其他编程语言一样非常简单。其中只有两个值 – true 或者 false 。例子如下

Live Demo

  1. fun main(args: Array<String>) {
  2. val letter: Boolean // defining a variable
  3. letter = true // Assinging a value to it
  4. println("Your character value is "+"$letter")
  5. }

输出

  1. Your character value is true

字符串

字符串是字符数组。类似 Java,他们天生就是不可变的。在 Kotlin 中我们有两种可用的字符串 - 一种是原始字符串;另一种是转义字符串。下例中我们会使用这两种字符串:

Live Demo

  1. fun main(args: Array<String>) {
  2. var rawString : String = "I am Raw String!"
  3. val escapedString : String = "I am escaped String!\n"
  4. println("Hello!" + escapedString)
  5. println("Hey!!" + rawString)
  6. }

上例中的转移字符串会在第一行输出之后多加一个额外的换行。完整输出如下:

  1. Hello!I am escaped String!
  2. Hey!!I am Raw String!

数组

数组是同类型数据的集合。像 java 一样,Kotlin 支持不同数据类型的数组。下例中我们使用整型数组:

Live Demo

  1. fun main(args: Array<String>) {
  2. val numbers: IntArray = intArrayOf(1, 2, 3, 4, 5)
  3. println("Hey!! I am array Example"+numbers[2])
  4. }

The above piece of code yields the following output. The indexing of the array is similar to other programming languages. Here, we are searching for a second index, whose value is “3”.

  1. Hey!! I am array Example3

Collections

Collection is a very important part of the data structure, which makes the software development easy for engineers. Kotlin has two types of collection - one is immutable collection (which means lists, maps and sets that cannot be editable) and another is mutable collection (this type of collection is editable). It is very important to keep in mind the type of collection used in your application, as Kotlin system does not represent any specific difference in them.
Live Demo fun main(args: Array) { val numbers: MutableList = mutableListOf(1, 2, 3) //mutable List val readOnlyView: List = numbers // immutable list println(“my mutable list—“+numbers) // prints “[1, 2, 3]” numbers.add(4) println(“my mutable list after addition —“+numbers) // prints “[1, 2, 3, 4]” println(readOnlyView)
readOnlyView.clear() // ⇒ does not compile
// gives error
}The above piece of code will yield the following output in the browser. It gives an error when we try to clear the mutable list of collection. main.kt:9:18: error: unresolved reference: clear readOnlyView.clear() // -> does not compile
^In collection, Kotlin provides some useful methods such as first(), last(), filter(), etc. All these methods are self-descriptive and easy to implement. Moreover, Kotlin follows the same structure such as Java while implementing collection. You are free to implement any collection of your choice such as Map and Set.
In the following example, we have implemented Map and Set using different built-in methods.
Live Demo fun main(args: Array) { val items = listOf(1, 2, 3, 4) println(“First Element of our list——“+items.first()) println(“Last Element of our list——“+items.last()) println(“Even Numbers of our List——“+items. filter { it % 2 = = 0 }) // returns [2, 4]

val readWriteMap = hashMapOf(“foo” to 1, “bar” to 2) println(readWriteMap[“foo”]) // prints “1”

val strings = hashSetOf(“a”, “b”, “c”, “c”) println(“My Set Values are”+strings) }The above piece of code yields the following output in the browser. First Element of our list——1 Last Element of our list——4 Even Numbers of our List——[2, 4] 1 My Set Values are[a, b, c]

Ranges

Ranges is another unique characteristic of Kotlin. Like Haskell, it provides an operator that helps you iterate through a range. Internally, it is implemented using rangeTo() and its operator form is (..).
In the following example, we will see how Kotlin interprets this range operator.

Live Demo

fun main(args: Array) { val i:Int = 2 for (j in 1..4) print(j) // prints “1234”

if (i in 1..10) { // equivalent of 1 < = i && i < = 10 println(“we found your number —“+i) } }The above piece of code yields the following output in the browser.

1234we found your number —2